please take a look at accept() method,
second and third parameters must have values
don't forget

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

int main(void)
{
    struct sockaddr_in local;
    struct sockaddr_in client;
    int *csize;
    int s;    int s1;    int rc; char buf[1];
    local.sin_family = AF_INET;
    local.sin_port = htons(5300);
    local.sin_addr.s_addr = htonl(INADDR_ANY);
   
    char getTime[255];

    //added by Justin
   
    printf("A server has been started, the port number is reserved as 5300\n\n");


    s = socket(PF_INET, SOCK_STREAM, 0);
    if(s<0){
        perror("socket call failed");
        exit(1);
    }

    rc = bind(s, (struct sockaddr *)&local, sizeof(local));
    if(rc<0){
        perror("bind call failure");
        exit(1);
    }


    printf("A server is waintg for a client request\n\n");

    rc = listen(s,5);
    if(rc){
        perror("listen call failed");
        exit(1);
    }



    csize = sizeof(client);        // this is used to get a ip address using accept()

    s1= accept(s,(struct sockaddr*)&client ,&csize);    // we need to know the ip address of a client
    // to get client address. then we have to send this client
    // address to the client.

    printf("Conntection established!\n\n");

    if(s1<0){
        perror("accept call failed");
        exit(1);
    }

    rc = recv(s1,getTime ,sizeof(getTime), 0);    // rc return the size of the data

    if(rc<=0){
        perror("recv call failed");
        exit(1);
    }

    printf("\nCurrent time from client : %s \n", getTime);        //print current Time

    printf("\n\nA server will send a client's IP address \n");

    rc = send(s1, &client , sizeof(client), 0);        // send a structure of sockaddr_in to the client
    if(rc <= 0)                        // if it failes
    {
        perror("send call failed");
        exit(0);
    }
}





'Programming > Network Programming' 카테고리의 다른 글

TCP programming basis  (0) 2011.10.28
.. no regret..  (0) 2011.10.26
get client IPadrress in server side  (0) 2011.10.10
Reference for unix socket api  (0) 2011.09.29
TCP/IP  (0) 2011.09.21
Posted by 박세범