Server not accepting client connection.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ppuniversal
    New Member
    • Feb 2007
    • 52

    Server not accepting client connection.

    Hi everyone,
    I have made a client-server code in Linux. The server is running properly and is sitting at its
    Code:
    accept()
    method. But the client is unable to connect to the server. The Server and Client codes are as follows :


    Filename : Server.cpp

    Code:
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netdb.h>
    #include<stdio.h>
    #include<stdlib.h>
    //#include<fcntl.h>
    #include<string.h>
    #include<netinet/in.h>
    #include<unistd.h>
    #include<sys/stat.h>
    #include<errno.h>
    
    
    #define SERVERPORT 10000
    #define MAXBUFF 1024
    
    main()
    {
    	int socket1,socket2;
    	int addrlen;
    	struct sockaddr_in ss,cc;
    	int retstatus=0;
    	int i,j,rcounter;
    
    	char *s;
    
    	char* bufptr;	
    	char buf[MAXBUFF];
    	char data[MAXBUFF];
    	
    	printf("in main of server");
    
    	socket1=socket(AF_INET,SOCK_STREAM,0);
    	printf("socket no :%d",socket1);
    	
    	ss.sin_family=AF_INET;	
    	ss.sin_addr.s_addr=INADDR_ANY;
    	ss.sin_port=htons(SERVERPORT);
    	
    	if(socket1==-1)
    	{
    		printf("Error in creating socket");
    		exit(1);
    	}
    	printf("socket created");
    	
    	printf("\nbind:%d",retstatus);
    	retstatus=bind(socket1,(struct sockaddr*)&ss,sizeof(struct sockaddr));
    	getchar();
    	printf("\nbind:%d",retstatus);
    	if(retstatus==-1)
    	{
    		printf("cannot bind to socket");
    		exit(1);
    	}
    
    	retstatus=listen(socket1,5);
    
    	if(retstatus==-1)
    	{
    		printf("cannot listen on socket");
    		exit(1);
    	}
    
    	//for(;;)
    	//{
    		
    
    		addrlen=sizeof(cc);
    		perror(s);
    		socket2=accept(socket1,(struct sockaddr*)&cc,(socklen_t*)&addrlen);
    		perror(s);
    
    	if(socket2==-1)
    	{
    		printf("cannot accept connection");
    		exit(1);
    	}
    //}
    	
    	if((rcounter=recv(socket2,data+i,strlen(data),0))>0)
    	{
    		i+=rcounter;
    	}
    	
    	if(rcounter==-1)
    	{
    		printf("could not read from socket");
    		j=shutdown(socket1,2);
    	}
    
    	//data[i+1]='/0';
    	printf("data is:%s",data);
    	
    	printf("server shutting down");
    	i=shutdown(socket1,2);
    
    }



    Filename : Client.cpp
    Code:
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<netdb.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/stat.h>
    #include<string.h>
    #include<errno.h>
    
    #define SERVERPORT	10000
    #define MAXBUFF	1024
    
    main(int argc,char* argv[])
    {
    	int sockd=0,i;
    	struct sockaddr_in ss;
    	int retstatus=0;
    	char buf[MAXBUFF];
    	char *s;
    
    	if(argc<3)
    	{
    		printf("something");
    		//exit(1);
    	}
    	
    	printf("in main of client");
    	sockd=socket(AF_INET,SOCK_STREAM,0);
    	printf("\n%d",sockd);
    	if(sockd==-1)
    	{
    		printf("Error in creating socket");
    		exit(1);
    	}
    	printf("Socket created");
    	ss.sin_family=AF_INET;	
    	ss.sin_addr.s_addr=inet_addr("127.0.0.1");
    	ss.sin_port=htons(SERVERPORT);
    
    	
    	retstatus=connect(sockd,(struct sockaddr*)&ss,sizeof(ss));
    	perror(s);
    	printf("after connect %d",retstatus);
    
    	if(retstatus==-1)
    	{
    		printf("Error in connecting ");
    		exit(1);
    	}
    	
    
    	retstatus=send(sockd,"hello"/*argv[2]*/,strlen("hello"/*argv[2]*/)+1,0);
    	if(retstatus==-1)
    	{
    		printf("could not send data");
    		exit(1);
    	}
    	
    	printf("client closing");
    	i=shutdown(sockd,2);
    }

    The client on execution prints -1 in this line :
    Code:
    printf("after connect %d",retstatus);
    .

    Waiting for reply.
    Pawan
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Is this Windows?? If not disregard my comments below.

    Read this.

    The -1 is only interesting. There are many returns from connect().

    The call looks odd:
    Originally posted by ppuniversal
    retstatus=conne ct(sockd,(struc t sockaddr*)&ss,s izeof(ss));
    You need to provide the socket which is a SOCKET and not an int.
    You need to provide the address of the socket name which is a struct sockaddr*.
    You need to provide the the length of the socket name and not the sizeof the name.

    I have no idea what struct sockaddr_in is for.

    Comment

    • ppuniversal
      New Member
      • Feb 2007
      • 52

      #3
      Originally posted by weaknessforcats
      Is this Windows?? If not disregard my comments below.

      Read this.

      The -1 is only interesting. There are many returns from connect().

      The call looks odd:


      You need to provide the socket which is a SOCKET and not an int.
      You need to provide the address of the socket name which is a struct sockaddr*.
      You need to provide the the length of the socket name and not the sizeof the name.

      I have no idea what struct sockaddr_in is for.

      This is in Linux. I have mentioned this at the beginning of the post. And I will try your suggestion and will reply accordingly.
      Thanks and Regards
      Pawan

      Comment

      Working...