wrong in Socket program

Discussion in 'C Programming' started by chellappa, Oct 24, 2005.

  1. chellappa

    chellappa Guest

    Hi,
    This is program for socket program for server and client ... but data
    send from client and receive by server but i can t sen d message fro m
    server to client ... check i mistake .. i treed .. its have some
    mistake.. if possible correct and send me

    server.c
    ==========
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>

    #define SERVERPORT 1500
    #define BUFFERLEN 250

    int main(void)
    {
    int sd,sd2;
    int rc,length,on=1;
    char buffer[BUFFERLEN];
    fd_set read_fd;
    struct timeval timeout;
    struct sockaddr_in serveraddr;
    do
    {
    sd=socket(AF_INET,SOCK_STREAM,0);
    if(sd <0)
    {
    perror("Socket Failed");
    break;
    }
    rc=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
    if (rc <0 )
    {
    perror(" SOCKEET OPTION FAILED");
    break;
    }
    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons(SERVERPORT);

    rc= bind(sd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
    if (rc <0)
    {
    perror("BIND PLEM");
    break;
    }
    rc=listen(sd,10);
    if(rc<0)
    {
    perror("Listem Plem");
    break;
    }
    printf("Server Ready\n");
    sd2=accept(sd,NULL,NULL);
    if(sd2<0)
    {
    perror("accpet failed");
    break;
    }
    length=BUFFERLEN;
    rc=setsockopt(sd2,SOL_SOCKET,SO_RCVLOWAT,(char
    *)&length,sizeof(length));
    if (rc < 0)
    {
    perror("SD2 OPT FAIL");

    }
    rc= recv(sd2,buffer,sizeof(buffer),0);
    if ( rc <0)
    {
    perror("recv() failed");
    break;
    }
    printf("%d bytes of data were received %s \n",rc,buffer);
    if ( rc == 0 || rc < sizeof(buffer))
    {
    printf(" The client closed the connection before all of \n");
    printf("data was sent\n");
    break;
    }
    strcpy(buffer,"SREE RAM");
    rc=send(sd2,buffer,sizeof(buffer),0);
    printf ("DAta Sented");
    }
    while(0);
    close(sd);
    close(sd2);
    }




    Client.c
    =========

    #include<stdio.h>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<netdb.h>

    #define SERVER_PORT 1500
    #define BUFFER_LENGTH 250
    #define FALSE 0
    #define SERVER_NAME "ServerHostName"

    int main(int argc,char *argv[])
    {
    int sd,rc,bytesReceived;
    char buffer[BUFFER_LENGTH];
    char *server=(char *)malloc(sizeof(char *));
    struct sockaddr_in serveraddr;
    struct hostent *hostp;

    do {
    sd=socket(AF_INET,SOCK_STREAM,0);
    if (sd < 0){
    perror("socket() failed");
    break;
    }

    if (argc >1)
    strcpy(server,argv[1]);
    else
    strcpy(server,SERVER_NAME);

    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(SERVER_PORT);
    serveraddr.sin_addr.s_addr = inet_addr(server);
    if (serveraddr.sin_addr.s_addr == (unsigned long) INADDR_NONE)
    {
    hostp=gethostbyname(server);
    if(hostp == (struct hostent *) NULL)
    {
    printf("Host Not Found ");
    printf("h_errno = %d\n",h_errno);
    break;
    }
    memcpy(&serveraddr.sin_addr,hostp->h_addr,sizeof(serveraddr.sin_addr));
    }
    else
    {
    printf ("INADDR FAIL");
    }
    rc=connect(sd, (struct sockaddr *) &serveraddr , sizeof(serveraddr));
    if (rc < 0)
    {
    perror("connect() failed");
    break;
    }

    memset(buffer, 's',sizeof(buffer));
    rc=send(sd,buffer,sizeof(buffer),0);
    printf("Data Sended");

    if ( rc < 0)
    {
    perror("send () failed");
    break;
    }
    bytesReceived = 0;
    while (bytesReceived < BUFFER_LENGTH);
    {
    rc= recv(sd, &buffer[bytesReceived],BUFFER_LENGTH -
    bytesReceived,0);
    printf("Data Recvived");
    if ( rc < 0)
    {
    perror("recv() failed");
    break;
    }
    else if (rc == 0)
    {
    printf("The Server Closed the connection\n");
    break;
    }
    printf("%c",buffer[bytesReceived]);
    bytesReceived += rc;


    }

    } while(FALSE);

    if (sd!=-1)
    close(sd);

    }
    Thanks advance
    chellappa, Oct 24, 2005
    #1
    1. Advertising

  2. chellappa wrote:
    > Hi,
    > This is program for socket program for server and client ... but data
    > send from client and receive by server but i can t sen d message fro m
    > server to client ... check i mistake .. i treed .. its have some
    > mistake.. if possible correct and send me
    >


    Troll? Fool? Slow learner?

    -David
    David Resnick, Oct 24, 2005
    #2
    1. Advertising

  3. Pseudo-well-meaning-foreigners (Was: Re: wrong in Socket program)

    In article <>,
    David Resnick <> wrote:
    >
    >chellappa wrote:
    >> Hi,
    >> This is program for socket program for server and client ... but data
    >> send from client and receive by server but i can t sen d message fro m
    >> server to client ... check i mistake .. i treed .. its have some
    >> mistake.. if possible correct and send me
    >>

    >
    >Troll? Fool? Slow learner?
    >
    >-David
    >


    Yup.
    Kenny McCormack, Oct 24, 2005
    #3
  4. chellappa

    Joe Estock Guest

    David Resnick wrote:
    > chellappa wrote:
    >
    >>Hi,
    >>This is program for socket program for server and client ... but data
    >>send from client and receive by server but i can t sen d message fro m
    >>server to client ... check i mistake .. i treed .. its have some
    >>mistake.. if possible correct and send me
    >>

    >
    >
    > Troll? Fool? Slow learner?
    >
    > -David
    >


    Around here we call them lunch or food ;)

    -Joe
    Joe Estock, Oct 24, 2005
    #4
  5. "chellappa" <> writes:
    > This is program for socket program for server and client ... but data
    > send from client and receive by server but i can t sen d message fro m
    > server to client ... check i mistake .. i treed .. its have some
    > mistake.. if possible correct and send me


    Chellappa, you have already been told that sockets are off-topic in
    this newsgroup. Try a newsgroup that's specific to the system you're
    using, perhaps comp.unix.programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,771
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    952
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    532
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    650
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    704
Loading...

Share This Page