Thread: Help With Socket Programming

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    21

    Help With Socket Programming

    Hello - I need some help in socket Programming - I have Server and client code which is working fine. When client type some thing - it appears on the server side.

    Now I have to do same thing - when server writes something it should appear on the client side too.
    Any help/suggestions are really appreciated.

    Here is client code :-

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>
    #include <strings.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define SERVER_PORT 5432
    #define MAX_LINE 256
    
    int main(int argc, char * argv[])
    {
    	FILE *fp;
    	struct hostent *hp;
    	struct sockaddr_in sin;
    	char *host;
    	char buf[MAX_LINE];
    	int s;
    	int len;
    
    	if (argc==2) {
    		host = argv[1];
    	} else  {
    		fprintf(stderr, "usage : simplex-talk hhost\n");
    		exit(1);
    	}
    
    
    	/* translare host name into Peer's IP address */
    	hp = gethostbyname(host);
    
    	if (!hp) {
    		fprintf(stderr, "Simplex-talk : unknown host : %s\n", host);
    		exit(1);
    	}
    
    
    	/* build address data structure */
    	bzero((char *)&sin, sizeof(sin));
    	sin.sin_family = AF_INET;
    	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    	sin.sin_port = htons(SERVER_PORT);
    
    	/*active open*/
    	if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    		perror ("Simplex-talk : socket");
    		exit(1);
    	}
    
    
    	if (connect(s, (struct sockaddr *)&sin , sizeof(sin)) < 0) {
    		perror ("simplex-talk: connect");
    		close(s);
    		exit(1);
    	}
    
    
    	/*main loop : get and send lines of text */
    	while (fgets(buf, sizeof(buf), stdin)) {
    		buf[MAX_LINE-1] = '\0';
    		len = strlen(buf) + 1;
    		send (s, buf, len, 0);
    	}
    }
    
    
    
    
    Here is Server code :-
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>
    #include <strings.h>
    #include <stdlib.h>
    
    #define SERVER_PORT 5432
    #define MAX_PENDING 5
    #define MAX_LINE 256
    
    int main()
    {
    	struct sockaddr_in sin;
    	char buf[MAX_LINE];
    	int len;
    	int s, new_s;
    
    	/* build address data structure*/
    	bzero((char *)&sin, sizeof(sin));
    	sin.sin_family = AF_INET;
    	sin.sin_addr.s_addr = INADDR_ANY;
    	sin.sin_port = htons(SERVER_PORT);
    
    	/*setup passive open */
    	if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    		perror ("Simplex-talk: socket");
    		exit(1);
    	}
    
    	if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) <0) {
    		perror ("Simplex * - talk :bind");
    		exit(1);
    	}
    
    	listen(s, MAX_PENDING);
    
    	/*wait fro connection , then receive and print text */
    	while(1) {
    		if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
    			perror("simplex-talk :accept");
    			exit(1);
    		}
    
    		while (len = recv(new_s, buf, sizeof(buf), 0))
    			fputs(buf, stdout);
    
    		close(new_s);
    	}
    }
    Please reply soon.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Networking/Device Communication forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    the same topic has been discussed several times, try to search the board or be more specific. If this is the multiplexing aspect which bothers you, look for documentation about select(), poll() and friends.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Root4 - my intentions are not to annoy anyone. My specific question is what/where should I change so that my server code can also send the text to client. I tried to make small change to make sure atleast its reaching to the point where server is able to read the text written on the server - but it didn't work. Here is the code (see last two lines).

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>
    #include <strings.h>
    #include <stdlib.h>
    
    #define SERVER_PORT 5432
    #define MAX_PENDING 5
    #define MAX_LINE 256
    
    int main()
    {
    	struct sockaddr_in sin;
    	char buf[MAX_LINE];
    	int len;
    	int s, new_s;
    
    	/* build address data structure*/
    	bzero((char *)&sin, sizeof(sin));
    	sin.sin_family = AF_INET;
    	sin.sin_addr.s_addr = INADDR_ANY;
    	sin.sin_port = htons(SERVER_PORT);
    
    	/*setup passive open */
    	if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    		perror ("Simplex-talk: socket");
    		exit(1);
    	}
    
    	if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) <0) {
    		perror ("Simplex * - talk :bind");
    		exit(1);
    	}
    
    	listen(s, MAX_PENDING);
    
    	/*wait fro connection , then receive and print text */
    	while(1) {
    		if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
    			perror("simplex-talk :accept");
    			exit(1);
    		}
    
    		while (len = recv(new_s, buf, sizeof(buf), 0))
    			fputs(buf, stdout);
    
    		close(new_s);
    	}
    
    	while (fgets(buf, sizeof(buf), stdin)) {
    	fprint ("Read");
    	}}

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    Unless I'm misunderstanding what you're trying to do, all you have to do to have the sever send data back to the client is use the send function, just like you're doing on the client side.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    the server has to wait incoming data either from the standard input, or from the client socket, if it's from the client socket you display it, if it's from the standard input, you send it to the client. There are several functions allowing to test from which stream data is available and I gave those in my previous post: select(), poll(), pselect()...
    Code:
    while(1) {
      select(...)
      if(from client) recv(...),printf(...)
      if(from stdin) read(0,buffer,max),send(client,buffer,...)
    }
    As i told, you will find many code examples in older threads.

  7. #7
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    Quote Originally Posted by namasteall2000 View Post

    /*wait fro connection , then receive and print text */
    while(1) {
    [B] if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
    perror("simplex-talk :accept");
    exit(1);
    }[B]

    while (len = recv(new_s, buf, sizeof(buf), 0))
    fputs(buf, stdout);

    close(new_s);
    }

    while (fgets(buf, sizeof(buf), stdin)) {
    fprint ("Read");
    }
    }

    [/code]

    my question:
    #1
    why accepting is put into a forever loop...???
    accepting is only done when you want to establish a connection...
    get it out...
    it is a blocking socket... your program will stop there...

    #2
    Code:
    
    	while (fgets(buf, sizeof(buf), stdin)) {
    	fprint ("Read");
    	}}
    you put it after while(1) loop...
    how can it be executed...???
    correct me if i'm wrong...


  8. #8
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    i want to add some more...
    may be you want to make a multi client server...
    that's why you put accepting in while(1) loop...
    that's ok...
    but...you must a separated thread to handle accepting and receiving...
    because it is blocking socket...

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    21

    Thanks

    Thank you all - finally my programming is working - server is sending data back to the client now.

    Once again appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  4. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM