helo
new here
i dont know if this is the right place to post this, so escuse me and correct me if it isnt.
as the title says i'm having loads of problems because of that..
i discovered to be the "read" instrucion thats giving me trouble, (if its a long string sent) and i dont understand that..
any ideas, tips?
code:
C server:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
char * lerMesg(int newSock)
{
char * temp;
char linha[7776];
int flag=1,size=0;
do
{
read(newSock, linha, 7775);
size=0;
do
{
if(size>7777)
break;
strcpy(temp, linha);
if(temp[size]=='\n'&&temp[size-1]=='\r'&&temp[size-2]=='\n'&&temp[size-3]=='\r')
{
flag=0;
}
size++;
}while(flag==1);
}while(flag==1);
temp[size-4]='\0';
printf("\nServidor: recebeu %s\n", temp);
return(temp);
}
void resposta(int newSock)
{
char rsp[1000];
write(newSock,"testring\n<xml>\r\n\r\n",strlen("testring\n<xml>\r\n\r\n"));
}
int main(int argc, char *argv[])
{
struct sockaddr_in me, from;
int newSock,sock=socket(AF_INET,SOCK_STREAM,0);
int adl=sizeof(me);
int aux,res,bytes;
char * temp;
char aux1[999];
int porta=atoi(argv[1]);
fflush(stdin);
fflush(stdout);
printf("---------------SERVIDOR (lexico? xD)!-------------\n");
printf("\tPorta: %d \n", porta);
bzero((char *)&me,adl);
me.sin_family=AF_INET;
me.sin_addr.s_addr=htonl(INADDR_ANY);
me.sin_port=htons(porta); /* porta local */
if(-1==bind(sock,(struct sockaddr *)&me,adl))
{close(sock);puts("Porta de servidor ocupada...");exit(1);}
listen(sock,5);
for(;;)
{
newSock=accept(sock,(struct sockaddr *)&from,&adl);
if(newSock!=-1) /* importante verificar */
{
res=fork();
if(res)
{
if(res== -1)
puts("Fork Failed...");
else
close(newSock);
}
else
{
puts("\t------------- Novo Cliente ------------\n");
/* Processo FILHO serve o cliente */
close(sock);
/*------------- LER DO CLIENTE ------------------ */
temp=lerMesg(newSock);
/*--------------- RESPONDER PARA O CLIENTE ---------------------*/
resposta(newSock);
printf("Servidor: Enviada resposta\n");
puts("\t------------- Cliente Terminado ------------\n");
close(newSock);
exit(0);
}
}
}
}
java client:
Code:
public static void main(String[] args) throws IOException
{
String nome="xml_teste.xml";
sClient sC=new sClient(9998 ,nome);
teste=sC.resposta();
}
public class sClient {
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
public void ligacao(int porta) throws IOException
{
InetAddress address = InetAddress.getByName("<server here>");
try {
kkSocket = new Socket(address, porta);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}
out.flush();
}
public void info(String file) throws IOException {
String fromUser;
String xpto;
out.flush();
fromUser=file+"\r\n\r\n";
System.out.println("Cliente a enviar: "+ fromUser);
if (fromUser != null)
out.println(fromUser);
out.flush();
}
public String resposta() throws IOException
{
String fromServer="";
int s;
char c;
out.flush();
while ( (s=in.read())!=-1)
{
c=(char)s;
fromServer+=c;
System.out.print(c);
if (fromServer.equals("\r\n\r\n"))
break;
}
termina();
return fromServer;
}
public void termina() throws IOException
{
out.close();
in.close();
kkSocket.close();
}
public sClient(int porta,String file) throws IOException
{
System.out.println("Cliente!\n");
ligacao(porta);
info(file);
}
}
please help me and..
ty vm in advance