tcpserver.c
#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"
#define PORT 4444
#define BUF_SIZE 2000
void main() {
struct sockaddr_in addr, cl_addr;
int sockfd, len, ret, newsockfd;
char buffer[BUF_SIZE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error creating socket!\n");
exit(1);
}
printf("Socket created...\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = PORT;
ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret < 0) {
printf("Error binding!\n");
exit(1);
}
printf("Binding done...\n");
printf("Waiting for a connection...\n");
listen(sockfd, 5);
len = sizeof(cl_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len);
if (newsockfd < 0) {
printf("Error accepting connection!\n");
exit(1);
}
printf("Connection accepted...\n");
memset(buffer, 0, BUF_SIZE);
ret = recv(newsockfd, buffer, BUF_SIZE, 0);
if(ret < 0) {
printf("Error receiving data!\n");
exit(1);
}
printf("Received data: %s\n", buffer);
ret = send(newsockfd, buffer, strlen(buffer), 0);
if (ret < 0) {
printf("Error sending data!\n");
exit(1);
}
printf("Sent data: %s\n", buffer);
close(sockfd);
close(newsockfd);
}
tcpclient.c
#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"
#include"netdb.h"
#define PORT 4444
#define BUF_SIZE 2000
int main(int argc, char**argv) {
struct sockaddr_in addr, cl_addr;
int sockfd, ret;
char buffer[BUF_SIZE];
struct hostent * server;
char * serverAddr;
if (argc < 2) {
printf("usage: client < ip address >\n");
exit(1);
}
serverAddr = argv[1];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error creating socket!\n");
exit(1);
}
printf("Socket created...\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(serverAddr);
addr.sin_port = PORT;
ret = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret < 0) {
printf("Error connecting to the server!\n");
exit(1);
}
printf("Connected to the server...\n");
memset(buffer, 0, BUF_SIZE);
printf("Enter your message: ");
scanf("%s", buffer);
ret = send(sockfd, buffer, strlen(buffer), 0);
if (ret < 0) {
printf("Error sending data!\n");
exit(1);
}
printf("Sent data: %s\n", buffer);
memset(buffer, 0, BUF_SIZE);
ret = recv(sockfd, buffer, BUF_SIZE, 0);
if(ret < 0) {
printf("Error receiving data!\n");
exit(1);
}
printf("Received data: %s\n", buffer);
return 0;
}
Output:
terminal 1
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ gcc tcpserver.c -o server
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ gcc tcpclient.c -o client
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ ./server
Socket created...
Binding done...
Waiting for a connection...
Connection accepted...
Received data: hi
Sent data: hi
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ p-laptop:~/coding/tcp_2sys$ gcc tcpclient.c -o client
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ ./server
Socket created...
Binding done...
Waiting for a connection...
Connection accepted...
Received data: hi
Sent data: hi
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$
terminal 2
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ ./client
usage: client
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$ ./client 192.168.0.4
Socket created...
Connected to the server...
Enter your message: hi
Sent data: hi
Received data: hi
dhanoopbhaskar@dhanoop-laptop:~/coding/tcp_2sys$
NB: Here 192.168.0.4
is the IP address of the system where tcpserver code is running