Header files inclusion:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
TCP Server side:
#define MYPORT 3456 /* the port users will be connecting to */
#define BACKLOG 10 /* number of pending connections */
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* client's address info */
int sin_size;
sockfd = socket(AF_INET, SOCK_STREAM, 0)
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); /* fill with my IP */
/* Set rest of the bits of the padding field to 0 */
memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero));
/* Bind the address struct to the socket */
bind(sockfd, (struct sockaddr *) & my_addr, sizeof(my_addr));
/* Listen on the socket */
listen(sockfd,BACKLOG)
/* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd,(struct sockaddr *)&their_addr, &sin_size)
printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
close(new_fd);
TCP Client side:
#define PORT 3490 /* the port client will be connecting to */
#define MAXDATASIZE 100 /* max number of bytes we can get at once */
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct sockaddr_in their_addr; /* client's address information */
he=gethostbyname(arguements) /* get the host info */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = inet_addr(ip address); /* fill with client IP */
/* Set rest of the bits of the padding field to 0 */
memset(their_addr.sin_zero, '\0', sizeof(their_addr.sin_zero));
/* connect to server */
connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
UDP Server side:
#define MYPORT 4950 /* the port users will be sending to */
#define MAXBUFLEN 100
int sockfd;
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* client's address information */
int addr_len, numbytes;
char buf[MAXBUFLEN];
sockfd = socket(AF_INET, SOCK_DGRAM, 0)
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
/* Set rest of the bits of the padding field to 0 */
memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero));
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
addr_len = sizeof(struct sockaddr);
numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len)
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",buf);
close(sockfd);
UDP Client side:
#define MYPORT 4950 /* the port users will be sending to */
int sockfd;
struct sockaddr_in their_addr; /* client's address information */
int numbytes;
sockfd = socket(AF_INET, SOCK_DGRAM, 0)
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = inet_addr(ip address); /* fill with client IP */
/* Set rest of the bits of the padding field to 0 */
memset(my_addr.sin_zero, '\0', sizeof(their_addr.sin_zero));
numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr));
printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr));
close(sockfd);
send, sendto, receive and receivefrom
send (sockfd,*mesg, mesglen,0);
sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof(struct
sockaddr));
recv (sockfd, *buf, len, o);
recvfrom(sockfd, buf, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len)