0
votes

I am doing this simple chat application between one client and a server by establishing the socket connection between them. I am able to run client and server on two different terminals on the same computer. Now, how can I run client and server on two different computers? I tried doing this my giving the server machine's IP address in place of the localhost "127.0.0.1" in server program. It didn't work.

Can someone let me know how to implement this. Also, how can I pass ip address and port number dynamically?

TCP Client

#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<stdlib.h>

int main()
{
    char buf[100];
    int k;
    int sock_desc;
    struct sockaddr_in client;
    memset(&client,0,sizeof(client));
    sock_desc=socket(AF_INET,SOCK_STREAM,0);

    if(sock_desc==-1)
    {
        printf("Error in socket creation");
        exit(1);
    }

    client.sin_family=AF_INET;
    client.sin_addr.s_addr=INADDR_ANY;
    client.sin_port=3002;

    k=connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
    if(k==-1)
    {
        printf("Error in connecting to server");
        exit(1);
    }

    while(1)
    {
        printf("\nEnter data to be send to server: ");
        fgets(buf,100,stdin);
        if(strncmp(buf,"end",3)==0)
            break;

        k=send(sock_desc,buf,100,0);
        if(k==-1)
        {
            printf("Error in sending");
            exit(1);
        }

        k=recv(sock_desc,buf,100,0);
        if(k==-1)
        {
            printf("Error in receiving");
            exit(1);
        }

        printf("Message got from server is : %s",buf);
    }
    close(sock_desc);
    exit(0);
    return 0;
}

TCP server

#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
    char buf[100];
    int k;
    socklen_t len;
    int sock_desc,temp_sock_desc;
    struct sockaddr_in server,client;

    memset(&server,0,sizeof(server));
    memset(&client,0,sizeof(client));

    sock_desc=socket(AF_INET,SOCK_STREAM,0);
    if(sock_desc==-1)
    {
        printf("Error in socket creation");
        exit(1);
    }

    server.sin_family=AF_INET;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    server.sin_port=3002;

    k=bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
    if(k==-1)
    {
        printf("Error in binding");
        exit(1);
    }

    k=listen(sock_desc,20);
    if(k==-1)
    {
        printf("Error in listening");
        exit(1);
    }

    len=sizeof(client);//VERY IMPORTANT
    temp_sock_desc=accept(sock_desc,(struct sockaddr*)&client,&len);
    if(temp_sock_desc==-1)
    {
        printf("Error in temporary socket creation");
        exit(1);
    }

    while(1)
    {
        k=recv(temp_sock_desc,buf,100,0);
        if(k==-1)
        {
            printf("Error in receiving");
            exit(1);
        }

        printf("Message got from client is : %s",buf);
        printf("\nEnter data to be send to client: ");

        fgets(buf,100,stdin);
        if(strncmp(buf,"end",3)==0)
            break;

        k=send(temp_sock_desc,buf,100,0);
        if(k==-1)
        {
            printf("Error in sending");
            exit(1);
        }
    }
    close(temp_sock_desc);

    exit(0);
    return 0;
}
1
You can pass the IP and Port as arguments to the main. The program shall take the IP and port as command line arguments.Prabhu
Please properly indent your code, it would be much more readable then.alk
You may test whether telnet or any other network program works between the two computers. There is a possibility that some firewall might be blocking communication between the two computers.kjohri

1 Answers

1
votes

You have the client sockaddr_in and server sockaddr_in in reverse. The client sockaddr_in structure should be:

client.sin_family=AF_INET;
client.sin_addr.s_addr=inet_addr("add server ip here");
client.sin_port=3002;

And the server sockaddr_in structure should be:

server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=3002;

Right now your server binds only on loopback and your client connects to INADDR_ANY.