Jump to content

trouble with executing a server/client c based program

- - - - -

  • Please log in to reply
7 replies to this topic

#1
tknmb1

tknmb1

    Newbie

  • Members
  • Pip
  • 3 posts
i was trying to execute a simple server/client c based application, i've executed the server on machine A and client on machine B (both running on linux ),even though the code is correct (i didn't write it down my self but many people said it works fine) the client keeps telling me it couldn't connect to the server.can anyone please tell me what is the problem
PS: i used both external and internal ip adresses of machine A,still nothing, but when i execute both client and server on the same machine and use 127.0.0.1 or my internal ip adress it works correctly !!!
here is the server code:
#include <stdio.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
    int simpleport=0;
    int simplesocket=0;
    int returnstatus;
    int simplechildsocket;
    struct sockaddr_in simpleserver;
    char buffer[256]="";
    if(argc!=2)
    {
        fprintf(stderr,"usage: %s  <port number>\n",argv[0]);
        exit(1);
    }
    /*create the socket*/
    simplesocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(simplesocket<0)
    {
        fprintf(stderr,"socket not created!\n");
        exit(1);
    }
    else
    fprintf(stderr,"socket created successfully\n");
    memset(&simpleserver,0,sizeof(simpleserver));
    simpleport=atoi(argv[1]);
    //create simpleserver
    simpleserver.sin_family=AF_INET;
    simpleserver.sin_addr.s_addr=htonl(INADDR_ANY);
    simpleserver.sin_port=htons(simpleport);
    //the binding process
    returnstatus=bind(simplesocket,(struct sockaddr*)&simpleserver,sizeof(simpleserver));
    if(!returnstatus)
    fprintf(stderr,"bind completed!\n");
    else
    {
        fprintf(stderr,"could not bind to socket!\n");
        close(simplesocket);
        exit(1);
    }
    fprintf(stderr,"%s\n",(char *)inet_ntoa(simpleserver.sin_addr));
    //listen on socket
    returnstatus=listen(simplesocket,5);
    if(returnstatus)
    {
        fprintf(stderr,"could not listen on socket\n");
        close(simplesocket);
        exit(1);
    }
    else
    fprintf(stderr,"listening on socket!\n");
    while(1)
    {
        struct sockaddr_in simpleclient= {0};
        int clientnamelength=sizeof(simpleclient);
        simplechildsocket=accept(simplesocket,(struct sockaddr *)&simpleclient,&clientnamelength);
        if(simplechildsocket<0)
        {
            fprintf(stderr,"could not accept connections!\n");
            close(simplechildsocket);
            exit(1);
        }
        else
        fprintf(stderr,"connection established!\n");
        char clientname[20];
        if(inet_ntop(AF_INET,&simpleclient.sin_addr.s_addr,clientname,sizeof(clientname))!=NULL)
        fprintf(stderr,"handling client %s  %d\n",clientname,ntohs(simpleclient.sin_port));
        else
        {
            fprintf(stderr,"could not receive client adress!\n");
            close(simplechildsocket);
            exit(1);
        }
	sleep(20);
        returnstatus=recv(simplechildsocket,buffer,255,0);
        if(returnstatus<0)
        {
            fprintf(stderr,"recv() failed!");
            close(simplechildsocket);
            exit(1);
        }
        int totalbytesent=0;
        while(returnstatus>0)
        {
            totalbytesent=send(simplechildsocket,buffer,returnstatus,0);
            if(totalbytesent<0)
            {
                fprintf(stderr,"send() failed");
                close(simplechildsocket);
                exit(1);
            }
            else if(totalbytesent!=returnstatus)
            {
                fprintf(stderr,"sent unexpectd number of bytes!\n");
                close(simplechildsocket);
                exit(1);
            }
            returnstatus=recv(simplechildsocket,buffer,255,0);
            if(returnstatus<0)
            {
                fprintf(stderr,"revc() failed\n");
                close(simplechildsocket);
                exit(1);
            }
        }
        close(simplechildsocket);
    }
    close(simplesocket);
    return 0;
}
and the client code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
int main(int argc,char *argv[])
{
    int simplesocket;
    int returnstatus;
    int portnumber;
    char *buffer, *servip;
    struct sockaddr_in simpleserver;
    size_t addr_len;
    if(argc!=4)
    {
        fprintf(stderr,"usage : %s <server adress> <echo word>  <server port>\n",argv[0]);
        exit(1);
    }
    buffer=(char*)malloc(strlen(argv[2])*sizeof(char));
    if(!buffer)
    {
        fprintf(stderr,"no free memory space!\n");
        exit(1);
    }
    simplesocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(simplesocket<0)
    {
        fprintf(stderr,"could not create socket!\n");
        exit(1);
    }
    else
    fprintf(stderr,"socket created successfully!\n");
     memset(&simpleserver,0,sizeof(simpleserver));
    portnumber=atoi (argv[3]);
    simpleserver.sin_port=htons(portnumber);
    servip=argv[1];
    returnstatus=inet_pton(AF_INET,servip,&simpleserver.sin_addr.s_addr);
    if(returnstatus==0)
    {
        fprintf(stderr,"inet_pton()failed,invalid IP adress!\n");
        close(simplesocket);
	exit(1);
    }
    else
    if(returnstatus<0)
    {
 	fprintf(stderr,"inet_pton()failed");
	close(simplesocket);
	exit(1);
    }
    simpleserver.sin_family=AF_INET;
    returnstatus=connect(simplesocket,(struct sockaddr*)&simpleserver,sizeof(simpleserver));
    if(returnstatus<0)
    {
        fprintf(stderr,"could not connect to  server!\n");
        close (simplesocket);
        exit(1);
    }
    else
    fprintf(stderr,"connect successfully!\n");
    strcpy(buffer,argv[2]);
    returnstatus=send(simplesocket,buffer,strlen(buffer),0);
    if(returnstatus<0)
    {
	fprintf(stderr,"send() failed");
	close(simplesocket);
	exit(1);
     }
    else if(returnstatus!=strlen(buffer))
    {
	fprintf(stderr,"send(), sent unexpected number of bytes");
	close(simplesocket);
	exit(1);
    }
    int totalbytesreceived=0;
    int echostrlen=sizeof(buffer);
    strcpy(buffer,"");
    while(totalbytesreceived<echostrlen)
    {
	returnstatus=recv(simplesocket,buffer,255,0);
	if(returnstatus<0)
	{
		fprintf(stderr,"recv()failed");
		close(simplesocket);
		exit(1);
	}
	else if(returnstatus==0)
	{
		fprintf(stderr,"connection closed prematurely");
		close(simplesocket);
		exit(1);
	}
	totalbytesreceived+=returnstatus;
	buffer[returnstatus]='\0';
	fputs(buffer,stdout);
	}
	fputc('\n',stdout);
	fprintf(stderr,"ok");
    close(simplesocket);
    return 0;
}

Edited by tknmb1, 23 June 2010 - 08:43 AM.


#2
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
the problem is that your client doesnt do a ip resolution , i other words you cant put in Google for the client but a exact IP of google you can find this by using ping of finger in terminal of your mashine

so ping Google will give you 74.125.79.104 and that is what you put in your program client

also program will not work if you do a localhost connect only as you said 127.0.0.1

and in the future please use code tags

to make your client work with domain names (www.google.com,www.facebook.com...) you need to use getaddrinfo() function google for explanation

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Please use the
 tags when posting code to make it easier to read. When posting, paste your code in, highlight it with the cursor and click on the button with the # on it.

As for ferovac's suggestion (nice catch, by the way) you should take a look at Beej's Guide to Network Programming. Awesome comprehensive guide, a little hard to wade through, but I'll be around to help out if necessary. Just let us know.
sudo rm -rf /

#4
tknmb1

tknmb1

    Newbie

  • Members
  • Pip
  • 3 posts
thanks for the reply, and sorry the code wasn't tagged (didn't know about that # thing)
the problem is that i want my client to connect to my server application that is running on another machine (laptop)
i don't want it to connect to google or facebook as you mentioned.

#5
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
that isnt easy as you think if possible at all, you probably have a router at home so i dont think that its possible to connect to a server behind that router, you would have to do some changes to configuration of you router and i think that you have to use raw socket to accomplish this

but i'm not sure about any of this never tried it my self so i cant say but i would like to know if its possible, and how is it possible???

i assume if remote connection is possible to your computer then this should be as well.

#6
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
got something for you:

link1

link2

link3

link4

unbelievable how easy it is, take a good look at the warning howtogeek is telling you , if you have important data on your mashine it can get stolen, if your app isnt safe

#7
tknmb1

tknmb1

    Newbie

  • Members
  • Pip
  • 3 posts
i just can't thank you enough for your help ferovac, you were absolutely right (excellent how-to by the way !)

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
+rep. :)
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users