Find the answer to your Linux question:
Results 1 to 5 of 5
I am trying to learn socket programming and decided to implement this simple program to understand the basic concepts :- Client.c #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<unistd.h> #include<string.h> ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    4

    Help with sockets


    I am trying to learn socket programming and decided to implement this simple program to understand the basic concepts :-

    Client.c
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<string.h>
    /*
    struct sockaddr_in
    {
    short sin_family;
    unsigned short sin_port;
    struct in_addr sin_addr;
    char sin_zero[8];
    }
    */
    #define SERVER_PORT 6880
    #define SERVER_IP "127.0.0.1"

    int main()
    {
    int sockfd,fd;
    struct sockaddr_in server;
    struct sockaddr_in client;
    char filename[25],buf[1000];
    int n,len;
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
    printf("Client socket create error");

    server.sin_family=AF_INET;
    server.sin_port=htons(SERVER_PORT);
    server.sin_addr.s_addr=inet_addr(SERVER_IP);


    if((connect(sockfd,(struct sockaddr*)&server,sizeof(server)))<0)
    {
    printf("Error establishing connection..\n");
    exit(0);
    }
    else
    printf("\n\nSuccessful in establishing the connection to server..!\n\n");

    printf("Enter name of the file required\t");
    scanf("%s",filename);
    if(write(sockfd,filename,strlen(filename))<0)
    printf("Error in sending file name");

    printf("\nFile name transfered to server\n");
    }

    Server.c
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<stdlib.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdio.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<string.h>
    /*
    struct sockaddr_in
    {
    short sin_family;
    unsigned short sin_port;
    struct in_addr sin_addr;
    char sin_zero[8];
    }
    */
    #define SERVER_PORT 6880
    #define SERVER_IP "127.0.0.1"
    int main()
    {
    int sockfd;// Socket descriptor on server
    int clientfd,n,fd,m,clength;
    struct sockaddr_in server;// Information about socket on server
    struct sockaddr_in client;
    char fname[25],buf[1000];
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)//Creating socket(domain,type,protocol)
    printf("Socket create error");

    server.sin_family=AF_INET;//Socket family
    server.sin_port=htons(SERVER_PORT);//For platform independence
    server.sin_addr.s_addr=inet_addr(SERVER_IP);//For platform independence

    if(bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)//Binding socket to server info
    printf("Error in Binding socket to structure");

    if(listen(sockfd,5)<0)//Server is listening on sockfd
    printf("Error in listening");

    printf("Server ready to accept connections..\n");

    for(; ; )
    {
    clength=sizeof(client);
    if(clientfd=accept(sockfd,(struct sockaddr*)&client,(socklen_t*)&clength)<0)//Accepting a connection from the client
    printf("Server unable to accept connections..\n");
    else
    printf("Server has accepted a connection from the client..\n");

    m=read(clientfd,fname,25);//Read the filename. Im guessing this is the problem because the program does not continue after this.

    printf("\nName of file received is %s\n",fname);
    printf("Length is %d\n",strlen(fname));
    printf("Number of bytes recieved is %d\n\n",m);
    }

    }


    The problem is that i am unable to receive the filename at the server. I guess that the client is able to write to the socket but I am not able to read from the socket. would be great if someone helped me out. Thank you!!

  2. #2
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    224
    Hello,

    First thing we need to know is what is the result of the read on the server side?

    Are you sure your sockets are establishing a connection? Make the client side wait for a go-ahead after establishing the connect(). Then use netstat -a |egrep tcp to see if you have both the server's listening socket (68... status "listen") and the client-server connection (status "established").

    Bear in mind that the write on the client side returning no error does not mean that the data was sent to the server and does not mean that it was received on the server side. It just means that the data was successfully handed off to the underware.

    Your scanf() on the server receiving side. TCP is a byte-oriented protocol with no concept of messaging. Just because you "sent" 20 bytes does not gaurentee that a read on the other side will get 20 bytes in that one read. In this case (local connection), the traffic never gets on the wire so you should be fine but bear it in mind for future.

    Cheers - VP

  3. #3
    Just Joined!
    Join Date
    Feb 2011
    Posts
    4

    Solved

    I changed :-
    if(clientfd=accept(sockfd,(struct sockaddr*)&client,(socklen_t*)&clength)<0)
    printf(.............);
    else
    printf(......);

    to :-

    clientfd=accept(sockfd,(struct sockaddr*)&client,(socklen_t*)&clength);
    if(clientfd<0)
    printf(..........);
    else
    printf(..........);

    AND IT WORKED!! BUT I STILL DONT KNOW WHAT THE PROBLEM WAS

  4. #4
    Just Joined!
    Join Date
    Feb 2011
    Posts
    4
    Thank YOu!

  5. #5
    Just Joined!
    Join Date
    Dec 2009
    Location
    California
    Posts
    98
    That fixed it because in the original code, you were assigning clientfd to the result of the comparison with the return value being less than 0 and not to the return value from the accept.

    Consider the following example:
    x=3>2;
    printf("%d",x);

    The above will print 0 which is equivalent to "false" in C.


    So, if you really wanted to do it all in one line of code, you should use an extra set of parens....

    if((clientfd=accept(sockfd,(struct sockaddr*)&client,(socklen_t*)&clength))<0)


    I prefer your solution of doing the comparison in a seperate line of code. Much easier to understand.

Posting Permissions

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