C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 06-19-2008, 05:24 PM   #1
Registered User
 
Join Date: May 2008
Posts: 1
C server Java cliente problem..

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

Last edited by nitroghost; 06-20-2008 at 05:31 AM.
nitroghost is offline   Reply With Quote
Old 06-19-2008, 09:22 PM   #2
Registered User
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 217
Yes i think you should post the code, because right now there is next to no help we can offer.
39ster is offline   Reply With Quote
Old 06-20-2008, 01:49 PM   #3
FOSS Enthusiast
 
Join Date: Jun 2008
Location: Germany
Posts: 64
you should use send() and recv() for tcp connections and sendto() and recvfrom() for udp connections.
Using read() and write() for network sockets is like using a spoon for digging

Code:
me.sin_addr.s_addr=htonl(INADDR_ANY);
you don't need htonl() here. assigning INADDR_ANY is sufficient.

and very important, you forgot to set sin_zero(!)
Code:
memset(me.sin_zero, '\0', sizeof(me.sin_zero));
and the last thing I've noticed is how you do the fork. I'm not sure if your's will work, but it should be like this:
Code:
int child = fork();
if(child < 0)
{
    /* the fork failed */
}
else if(child > 0)
{
    /* this is done by the parent */
}
else if(child == 0)
{
    /* this is done by the child (i.e. the fork) */
}
mkruk is offline   Reply With Quote
Reply

Tags
base c, java, problem, read

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Programming Problem!!!! bobthebullet990 Networking/Device Communication 2 02-21-2008 07:36 PM
Interesting Problem with IOCP and AcceptEx() :: Winsock kuphryn Networking/Device Communication 0 10-07-2003 09:16 PM
JAVA : problem with terminalio CobraCC Windows Programming 1 09-29-2003 08:42 PM
socket question Unregistered C Programming 3 07-19-2002 01:54 PM
win32 Winsock problem spoon_ Windows Programming 3 07-19-2002 01:19 AM


All times are GMT -6. The time now is 03:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22