Results 1 to 2 of 2
  1. #1
    pimmling is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Simple Socket program: Java Client- C server

    Dear All,

    I am pasting my code for a simple socket server in C and a java client.

    I use write method sending character by character in Java. However after the chunk of characters are sent(here, 'h', 'e', 'y') , the Java client is send blocked as the C server does not reply to it :(

    I am assuming there is some problem with sending a null character(from the Java write) that would stop the recv at the C side.

    Any help would be greatly appreciated.

    Here is the C server:
    Java Code:
    #include <stdio.h>  	/* standard in and output*/
    #include <sys/socket.h> /* for socket() and socket functions*/
    #include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
    #include <stdlib.h>    
    #include <string.h>     
    #include <unistd.h>     /* for close() */
    
    
    int main(int argc, char *argv[]){
    	int sock, connected, bytes_received, true = 1;  
        char recv_data;      
    	char replyBuffer[32];
    	   
        struct sockaddr_in server_addr,client_addr;    
        int sin_size;       
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("Socket");
            exit(1);
        }
    	
        if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
            perror("Setsockopt");
            exit(1);
        }
            
        server_addr.sin_family = AF_INET;         
        server_addr.sin_port = htons(2400);     
        server_addr.sin_addr.s_addr = INADDR_ANY; 
        bzero(&(server_addr.sin_zero),8); 
    
        if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
            perror("Unable to bind");
                exit(1);
        }
    
        if (listen(sock, 5) == -1) {
            perror("Listen");
            exit(1);
        }
    		
    	printf("\nTCPServer Waiting for client on port 2400");
    
       while(1){  
            sin_size = sizeof(client_addr);
            connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
            printf("\n Got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
    
    
          	while ((bytes_received = recv(connected,&recv_data,1,0)) > 0){
    		printf("\nrecv= %c\n", recv_data);
    		}
    		int success = 1;
    		sprintf(replyBuffer, "%d", success);	
    		printf("reply buffer = %s\n", replyBuffer);
    		if (send(connected, replyBuffer, strlen(replyBuffer), 0) == -1)
    				perror("send() failed"); 
    				success = 0;
    		close(connected);
    	}
    }

    Here is the Java Client:
    Java Code:
         import java.net.*;
            import java.io.*;
            
            public class Client1
            {
            	public static void main(String[] args) throws IOException {
            		if (args.length < 2) {
            			System.err.println("Usage: java Client1 <IP address> <Port number>");
            			System.exit(0);
            		}
            		BufferedReader in = null;
            		OutputStream out = null;
            		Socket sock = null;
            
            		try {
            			sock = new Socket(args[0], Integer.parseInt(args[1]));
            			out = sock.getOutputStream();
            			in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            
            				String line = "hey";
            				String responseline = null;
            char[] strArray;
            strArray = line.toCharArray();
            
            while (true) {
            for( int index = 0; index < strArray.length; index++){
            	out.write(strArray[index]);
            }
            out.flush();
            System.out.println("data sent " );
            System.out.println("val returned"+in.readLine());
            			}
            		}
            		catch (IOException ioe) {
            			System.err.println(ioe);
            		}
            		finally {
            			if (in != null)
            				in.close();
            
            			if (out != null)
            				out.close();
            			if (sock != null)
            				sock.close();
            		}
            	}
            }

  2. #2
    spygas is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi, pimmling.

    I think your Java client application is not send-blocket, but receive-blocket. The statement
    Java Code:
    in.readLine()
    waits for the end-of-line constant during the read, which is not sent by your C server. Try this in your server app:
    Java Code:
        ...
        memset(replyBuffer, 0, sizeof replyBuffer); // clear buffer after previous usage
        sprintf(replyBuffer, "%d\n", success); // value + EOL constant
        ...
        if (send(connected, replyBuffer, strlen(replyBuffer) + 1, 0) == -1) // send string + 0 byte, which indicates the end of the string (ASCIIZ)
        ...
    Btw, why don't you send and receive whole string in your client app like this:
    Java Code:
    out.writeLine(line);
    ?
    Last edited by spygas; 11-09-2010 at 01:17 AM.

Similar Threads

  1. Simple Client (2) - Server program
    By Reztem in forum New To Java
    Replies: 2
    Last Post: 01-09-2012, 03:05 AM
  2. Java Socket server with C client problem
    By rnvrnv in forum Networking
    Replies: 6
    Last Post: 11-09-2010, 01:47 AM
  3. Replies: 0
    Last Post: 04-05-2009, 04:59 PM
  4. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 07:49 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •