0

The server has to echo the message sent by the client using C program in Linux. I'm using Ubuntu OS (I don't know whether this information is useful or not!). It worked for the first time. But for the second time, it gave 'Error Connection'. I tried changing port numbers. But still it didn't work.

server.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>

int main()
{
    int sd, sd1, len, confd, n;
    struct sockaddr_in ser, cli;
    char msg[50];

    if((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
        printf("\nSocket creation error\n");

    bzero(&ser, sizeof(ser));
    ser.sin_family = cli.sin_family = PF_INET;
    ser.sin_port = htons(10000);
    ser.sin_addr.s_addr = htonl(INADDR_ANY);
    len = sizeof(ser);

    if ((bind(sd, (struct sockaddr*)&ser, len)) < 0) {
        printf("\nBind Error");
        exit(0);
    }
    if (listen(sd, 2) == 0) {
        if ((sd1 = accept(sd, (struct sockaddr*)&ser, &len)) > 0) {
            do {
                bzero(&msg, 50);
                read(sd1, msg, 50);
                //int m=(int)msg;
                printf("\nMessage from client:%s\n", msg);
                write(sd1, msg, strlen(msg));
                if(strcmp(msg, "exit") == 0)
                    break;
            } while(strcmp(msg, "exit") != 0);
        }
    }
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>

int main()
{
    int sd, n, len;
    struct sockaddr_in ser, cli;
    char text[50];

    if ((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
        printf("\nSocket creation error\n");

    bzero(&ser, sizeof(ser));
    ser.sin_family = cli.sin_family = PF_INET;
    ser.sin_port = htons(10000);
    ser.sin_addr.s_addr = htonl(INADDR_ANY);
    len = sizeof(ser);
    
    if ((connect(sd, (struct sockaddr*)&ser, len)) < 0) {
        printf("\nError connection");
        exit(0);
    }
    while(1) {
        strcpy(text, " ");
        printf("\nEnter data which is to be sent:");
        scanf("%s", text);
        write(sd, text, strlen(text));
        read(sd, text, 50);
        printf("\nEcho msg from server:%s", text);
        if (strcmp(text, "exit") == 0)
            break;
    }
    close(sd);
}
1
  • 1
    1) you really should indent your code and 2) when connect fails info about the problem can be queried via the errno variable, as explained in the manpage. You should, upon failure, create an error message using the strerror function, that should help you (and us) understand what went wrong. Commented Oct 24, 2012 at 17:49

1 Answer 1

1

Can your client really connect to any address?

ser.sin_addr.s_addr=htonl(INADDR_ANY);

Most likely you meant to connect to a specific server:

ser.sin_addr.s_addr=inet_addr("127.0.0.1");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.