Socket Programming

can any one demonstrate a full function of client-server application that sends a "Hello World" string from the client side to the server side of the application..

please advice..
For MFC applications, you may find answer here: http://msdn.microsoft.com/en-us/library/t7a47kk4%28v=VS.100%29.aspx
The example that helped me: http://www.codeproject.com/KB/IP/MFCSockets.aspx

Note that CAsyncSockets are good for easy implementation because, for example, you won't need to use threads and won't need to specify the server address in a special object (Simple CString with IPv4 address works); but may not be as good for critical work: I've found bugs in the error handler and had other difficulties which I commented here: http://www.cplusplus.com/forum/windows/44129/ .
UDP OR TCP??!!!?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    #define PLATFORM_WINDOWS  1
    #define PLATFORM_MAC      2
    #define PLATFORM_UNIX     3

    #if defined(_WIN32)
    #define PLATFORM PLATFORM_WINDOWS
    #elif defined(__APPLE__)
    #define PLATFORM PLATFORM_MAC
    #else
    #define PLATFORM PLATFORM_UNIX
    #endif

int udpsock(int port = 0, const char* addr = INADDR_ANY);
string recvudp(int sock,const int size);
int sendudp(string str, string ip, unsigned short port, int sock);


int udpsock(int port, const char* addr){
    int handle = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
    if (handle < 1)
        return -1;

    sockaddr_in address;
    address.sin_family = AF_INET;
    if (addr == INADDR_ANY)
        address.sin_addr.s_addr = INADDR_ANY;
    else
        address.sin_addr.s_addr = inet_addr(addr);
    address.sin_port = htons( (unsigned short) port );

    if ( bind( handle, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 )
        return -1;

    return handle;
}
string recvudp(int sock,const int size){
    sockaddr_in SenderAddr;
    int SenderAddrSize = sizeof (SenderAddr);
    char buf[size];

    int retsize = recvfrom(sock, buf, sizeof(buf), 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
    if (retsize == -1){
        if (WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == 0){
         return "";
        }
        return "\0";
    }
    else if (retsize < size){
        buf[retsize] = NULL;
    }
    return buf;
}
int sendudp(string str, string ip, unsigned short port, int sock){
    sockaddr_in dest;
    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = inet_addr( ip.c_str() );
    dest.sin_port = htons( port );
    return sendto(sock,str.c_str(),str.size(),0, (sockaddr*)&dest,sizeof(dest));
}


To use these simple functions:

1
2
3
4
5
6
7
8
//CLIENT
WSADATA WsaData;
WSAStartup( MAKEWORD(2,2), &WsaData );

int socket = udpsock();
sendudp("hello world","127.0.0.1",27014,socket);

closesocket(socket);


1
2
3
4
5
6
7
8
//SERVER
WSADATA WsaData;
WSAStartup( MAKEWORD(2,2), &WsaData );

int socket = udpsock(27014);
cout << recvudp(socket,255);

closesocket(socket);



MAGIC untested code
--
i'll try.. thanks for the reply
Topic archived. No new replies allowed.