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
#define CLADDR_LEN 100
void main() {
struct sockaddr_in addr, cl_addr;
int sockfd, len, ret, newsockfd;
char buffer[BUF_SIZE];
pid_t childpid;
char clientAddr[CLADDR_LEN];
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);
for (;;) { //infinite loop
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");
inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN);
if ((childpid = fork()) == 0) { //creating a child process
close(sockfd);
//stop listening for new connections by the main process.
//the child will continue to listen.
//the main process now handles the connected client.
for (;;) {
memset(buffer, 0, BUF_SIZE);
ret = recvfrom(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, &len);
if(ret < 0) {
printf("Error receiving data!\n");
exit(1);
}
printf("Received data from %s: %s\n", clientAddr, buffer);
ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len);
if (ret < 0) {
printf("Error sending data!\n");
exit(1);
}
printf("Sent data to %s: %s\n", clientAddr, buffer);
}
}
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(s): ");
while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
ret = sendto(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &addr, sizeof(addr));
if (ret < 0) {
printf("Error sending data!\n\t-%s", buffer);
}
ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL);
if (ret < 0) {
printf("Error receiving data!\n");
} else {
printf("Received: ");
fputs(buffer, stdout);
printf("\n");
}
}
return 0;
}
18 Comments
how to execute this program
ReplyDelete--terminal 1--
Deletegcc tcpserver.c -o server
./server
--terminal 2--
gcc tcpclient.c -o client
./client 192.168.0.4
--terminal 3--
./client 192.168.0.4
Note that 192.168.0.4 is the IP address of the machine in which the server is running.
Deletethis is not tcp.this is udp.u have used "sendto()" and "recvfrom()"
ReplyDeleteHi,
DeleteSorry for late reply!
Functions sendto() and recvfrom() are MUST while using UDP. However you can use it in TCP also.
What distinguishes TCP from UDP is the following line of statement-
sockfd = socket(AF_INET, SOCK_STREAM, 0); //for TCP
sockfd = socket(AF_INET, SOCK_DGRAM, 0); //for UDP
we executed both client & server programs but, while we executing after giving corresponding ip address "Error connecting to the server" message is displayed.
ReplyDelete--terminal 1--
Deletegcc tcpserver.c -o server
./server
--terminal 2--
gcc tcpclient.c -o client
./client 192.168.0.4
--terminal 3--
./client 192.168.0.4
Replace 192.168.0.4 with the IP address of the machine in which the server program is running.
Thank you.. It works for us.
ReplyDeletei want to run it in ubuntu.. how to connect the server to multiple clients.
ReplyDeletethank you in advance
--terminal 1--
Deletegcc tcpserver.c -o server
./server
--terminal 2--
gcc tcpclient.c -o client
./client 192.168.0.4
--terminal 3--
./client 192.168.0.4
Replace 192.168.0.4 with the IP address of the machine in which the server program is running.
in the client terminal after writing ./client
Deleteit is showing that
no such file or directory is found
gcc tcpclient.c -o client
DeleteThis command compiles "tcpclient.c" and saves the executable with file name "client"
For execution "./client" can be used only if the name of the executable file generated on compiling the program is "client"
yes its working good
ReplyDeletethank you sir
thanks its working fine
ReplyDeletehow does the server diffrentiates between clienta if the port no and ip address are same?
ReplyDeleteIn TCP, you can only have one application listening to the same port at one time (in the same machine/IP address).
Deletewhat happen if we use ip address 122.0.0.1
ReplyDeleteYou can use 122.0.0.1 only if the IP address of the machine in which the server program is running is 122.0.0.1
Delete