Thread: Client/Server

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    29

    Client/Server

    Hi, me AGAIN!

    Having trouble getting an integer sent from my client to my server...everything compiles nicely, but nothing seems to be getting recieved....

    As soon as I can get some data being sent back and forth, I am well way...

    Client Code

    Code:
    /*Socket*/
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    
    int sd;
    
    int main(int argc,char** argv)
    {
    
    	int a = 0;	
    
    	sd = socket(AF_INET, SOCK_STREAM,0);
    /*End of Socket*/			
    
    	/*Client*/
    	struct sockaddr_in servaddr;
    
    	bzero(&servaddr,sizeof(servaddr));
    	servaddr.sin_family = AF_INET; //Set the domain
    	servaddr.sin_port = htons(atoi(argv[2])); //The port to connect to 
    	inet_aton(argv[1],&servaddr.sin_addr); //THe IP address of the server machine.
    
    //e.g Client   127.0.0.1   5000
    
    	connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr)); 
    
    	
    	printf("Please enter the integer you wish to send ");	
    	scanf("%d",  &a);
    	send(sd, &a, sizeof(a), 0);
    
    	/*End of Client*/
    }
    
    
    
    //SOCK_STREAM creates a realiable two-way connection
    //AF_INET specifies IPv4 Internet protocols
    //s is the descriptor (sd)
    //buf is the data to send (e.g a string)
    //len is the length of data sent
    //flags are the options(in this case can be 0).
    This is my server
    Code:
    /*Socket*/
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    
    int sd = 0;
    
    int main(int argc,char** argv)
    {
    	int connfd = 0;
    	int listenfd = 0;
    	size_t clilen;
    	int recv_data = 0; //recieving integer data
    	
    	sd = socket(AF_INET, SOCK_STREAM,0); //Creating New Socket
    	
    
    	listenfd = sd;//Did this as listenfd has not been defined yet 
    /*End of Socket*/			
    
    	/*Server*/
    	struct sockaddr_in servaddr; //Creating buffer
    
    	bzero(&servaddr,sizeof(servaddr)); //Sets all values in buffer to zero
    	servaddr.sin_family = AF_INET; //Sets the domain
    	servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //Accepting connections from any machine
    	servaddr.sin_port = htons(atoi(argv[1])); //First argument, e.g Server 50,000 uses port 50,000
    
    	bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); //Binds a socket to an address
    	listen(listenfd,10); //Accepts 10 clients to connect
    
    int store;
    //infinite for loop HERE
    //used to accept connections from client
    	for(;;) {
    	struct sockaddr_in cliaddr;
    	connfd = accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); //-1 if nothing is being received
    	store = recv(sd, &recv_data, sizeof(recv_data), 0);
    	if (connfd != -1)
    	{
    		printf("%d", store);
    	}
    	/*End of Client*/		
    	}
    
    }
    
    
    
    //SOCK_STREAM creates a realiable two-way connection
    //AF_INET specifies IPv4 Internet protocols
    //listenfd is a descriptor for the servers socket(intiated when creating the socket)


    I assume that when I return information back from the server, I 'send' from the server, and 'recv' at the client...

    Does this mean I'd need another infinite for loop at the client, always checking wther information is being sent back or not?

    Thanks
    Last edited by zonf; 12-14-2005 at 09:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RE: client/server shared memory problem
    By hampycalc in forum C Programming
    Replies: 0
    Last Post: 03-10-2006, 02:26 PM
  2. Client/server won't connect
    By Lateralus in forum Networking/Device Communication
    Replies: 0
    Last Post: 06-15-2005, 07:48 AM
  3. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM
  4. client/server program
    By purple in forum C Programming
    Replies: 3
    Last Post: 11-01-2002, 08:52 PM
  5. Client/Server Programming!!!
    By Silverdream in forum C Programming
    Replies: 4
    Last Post: 03-30-2002, 12:19 AM