Write a basic Echo Client-Server Application to create both an
Echo Client and an Echo Server in C using TCP Sockets. The client and
server implement the echo service while running on different Linux
machines and communicating with each other using TCP.
Hello friends ,this post contain a simple socket programming in c which echo the client message to the client(i.e. server will receive client message and send same message to the client i.e. echo server and client)
note : if you are beginner in computer network socket programming so i recommend to you please download and read first following pdf and then go through c code .
The Echo Server:After connecting to the basic Echo client, the basic Echo server (which is started first) simply echoes the string it receives back to the client, disconnects and terminates. The strings sent and received must be displayed on both client and server consoles.
The Echo Client:The basic Echo client connects to the Echo server and sends its data to the server. The data that the client sends is a string provided as the second client command-line argument.
server.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#define MAX_SIZE 1024
int main()
{
int sockfd,newsockfd;
socklen_t len;
char buffer[1024];
struct sockaddr_in serveraddr,clientaddr;
sockfd=socket(PF_INET,SOCK_STREAM,0);
serveraddr.sin_family=PF_INET;
serveraddr.sin_port=htons(12345);
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
listen(sockfd,1024);
printf("\n\n Bind Done");
printf("\n\n Waiting for Client's Message.............");
printf("\n ");
len = sizeof(clientaddr);
newsockfd = accept(sockfd,(struct sockaddr*)&clientaddr,&len);
recv(newsockfd,buffer,MAX_SIZE,0);
printf("\n Client's Message : %s\n",buffer);
send(newsockfd,buffer,MAX_SIZE,0);
memset(buffer,'\0', MAX_SIZE);
return 0;
}
client.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<stdio_ext.h>
#include<arpa/inet.h>
#include<ctype.h>
#define MAX_SIZE 2048
#define QUEUE_LIMIT 12
int main()
{
int clientsocket,count=0;
char buffer[MAX_SIZE];
struct sockaddr_in serverAddress;
clientsocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
serverAddress.sin_family=AF_INET;
serverAddress.sin_port=htons(12345);
serverAddress.sin_addr.s_addr=inet_addr("127.0.0.1");
memset(buffer,'\0', MAX_SIZE);
memset(buffer,'\0', MAX_SIZE);
connect(clientsocket,(struct sockaddr *) &serverAddress,sizeof(serverAddress));
printf("\nEnter string:");
scanf("%s",buffer);
char *sms=buffer;
send(clientsocket,sms,MAX_SIZE,0);
memset(buffer,'\0', MAX_SIZE);
recv(clientsocket,buffer,MAX_SIZE,0);
printf("\n Message from server: %s\n",buffer);
memset(buffer,'\0', MAX_SIZE);
return 0;
}
note :- first run server program on terminal and then client on ano
good
ReplyDeleteLooking for the Best Minecraft Server to play on? Want to advertise your minecraft server? Find them right here on our Minecraft Multiplayer Servers List. Cheap Minecraft Advertising
ReplyDelete