Thread: C server Java cliente problem..

  1. #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.

  2. #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.

  3. #3
    FOSS Enthusiast
    Join Date
    Jun 2008
    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) */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread