(Edit) Client-Server Example Using TCP
(Edit) Client-Server Example Using TCP
The Transmission Control Protocol (TCP) provides the concept of a connection, which is
a stateful network association between two hosts with a variety of error correction and
performance features. A process creates a TCP socket by calling the socket() function
with the parameters for the protocol family (PF_INET, PF_INET6), SOCK_STREAM (Stream
Sockets) and the IP protocol identifier IPPROTO_TCP.
[edit] Server
/* Server code in C */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
struct sockaddr_in stSockAddr;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(-1 == SocketFD)
{
perror("can not create socket");
exit(EXIT_FAILURE);
}
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
stSockAddr.sin_addr.s_addr = INADDR_ANY;
for(;;)
{
int ConnectFD = accept(SocketFD, NULL, NULL);
shutdown(ConnectFD, SHUT_RDWR);
close(ConnectFD);
}
return 0;
}
[edit] Client
/* Client code in C */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
struct sockaddr_in stSockAddr;
int Res;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == SocketFD)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
}
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
Res = inet_pton(AF_INET, "192.168.1.3", &stSockAddr.sin_addr);
if (0 > Res)
{
perror("error: first parameter is not a valid address family");
close(SocketFD);
exit(EXIT_FAILURE);
}
else if (0 == Res)
{
perror("char string (second parameter does not contain valid
ipaddress");
close(SocketFD);
exit(EXIT_FAILURE);
}
shutdown(SocketFD, SHUT_RDWR);
close(SocketFD);
return 0;
}
UDP address space, the space of UDP port numbers (in ISO terminology, the TSAPs), is
completely disjoint from that of TCP ports.
[edit] Server
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h> /* for close() for socket */
#include <stdlib.h>
int main(void)
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa;
char buffer[1024];
size_t fromlen, recsize;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(7654);
This infinite loop receives any UDP datagrams to port 7654 using recvfrom(). It uses
the parameters:
• socket
• pointer to buffer for data
• size of buffer
• flags (same as in recv or other receive socket function)
• address struct of sending peer
• length of address struct of sending peer.
[edit] Client
A simple demo to send a UDP packet containing "Hello World!" to address 127.0.0.1,
port 7654 might look like this:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h> /* for close() for socket */
In this code, buffer provides a pointer to the data to send, and buffer_length specifies
the size of the buffer contents.