#include"stdio.h" #include"stdlib.h" #include"sys/types.h" #include"sys/socket.h" #include"string.h" #include"netinet/in.h" #include"pthread.h" #define PORT 4444 #define BUF_SIZE 2000 #define CLADDR_LEN 100 void * receiveMessage(void * socket) { int sockfd, ret; char buffer[BUF_SIZE]; sockfd = (int) socket; memset(buffer, 0, BUF_SIZE); for (;;) { ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL); if (ret < 0) { printf("Error receiving data!\n"); } else { printf("client: "); fputs(buffer, stdout); //printf("\n"); } } } void main() { struct sockaddr_in addr, cl_addr; int sockfd, len, ret, newsockfd; char buffer[BUF_SIZE]; pid_t childpid; char clientAddr[CLADDR_LEN]; pthread_t rThread; 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); } inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN); printf("Connection accepted from %s...\n", clientAddr); memset(buffer, 0, BUF_SIZE); printf("Enter your messages one by one and press return key!\n"); //creating a new thread for receiving messages from the client ret = pthread_create(&rThread, NULL, receiveMessage, (void *) newsockfd); if (ret) { printf("ERROR: Return Code from pthread_create() is %d\n", ret); exit(1); } while (fgets(buffer, BUF_SIZE, stdin) != NULL) { ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len); if (ret < 0) { printf("Error sending data!\n"); exit(1); } } close(newsockfd); close(sockfd); pthread_exit(NULL); return; }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" #include"pthread.h" #define PORT 4444 #define BUF_SIZE 2000 void * receiveMessage(void * socket) { int sockfd, ret; char buffer[BUF_SIZE]; sockfd = (int) socket; memset(buffer, 0, BUF_SIZE); for (;;) { ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL); if (ret < 0) { printf("Error receiving data!\n"); } else { printf("server: "); fputs(buffer, stdout); //printf("\n"); } } } int main(int argc, char**argv) { struct sockaddr_in addr, cl_addr; int sockfd, ret; char buffer[BUF_SIZE]; char * serverAddr; pthread_t rThread; 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 messages one by one and press return key!\n"); //creating a new thread for receiving messages from the server ret = pthread_create(&rThread, NULL, receiveMessage, (void *) sockfd); if (ret) { printf("ERROR: Return Code from pthread_create() is %d\n", ret); exit(1); } 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); } } close(sockfd); pthread_exit(NULL); return 0; }
terminal 1
gcc tcpserver.c -o server -pthread
./server
terminal 2
gcc tcpclient.c -o client -pthread
./client 192.168.0.4
18 Comments
Great job. By the way it will be great if it has a way to exit properly
ReplyDeletethe code works awesome dude!!!
ReplyDeletesay bye to exit....how?
ReplyDeleteI'm getting message "Error binding!" when running the client code. Not sure why.
ReplyDeletewhen running client code, get error "Error binding!". Would you know why?
ReplyDeleteI just submitted a comment that I got "Error binding!" when running the client. When I tried it again, it occurred when running the server. Could it be occurring because I am running both client and server from the same machine?
ReplyDeleteI do not get "Error binding" any more. I was able to get it to work after I provide the correct IP parameter to the client. I found out the IP of the server by typing "hostname" and doing a ping. Then I used that IP as the parameter to ./client.
ReplyDeleteWorks great! Thank you so much.
Yeah! I owe my respect to you!
ReplyDeletewhich compiler should i use ?? mine is showing errors
ReplyDeleteGNU C Compiler (GCC)
Deletewhich IP address I need to give when I execute the client program
ReplyDeleteWhich IP address I am supposed to give while executing client program
ReplyDeleteServer IP address. (The IP address of the machine where the server program is running).
Deleteproblem copying the code, from where can i download the same
ReplyDeleteYou can do only copy and paste operations. It is not hosted anywhere else to download.
DeleteThank you.
Thanks buddy..
ReplyDeleteThanks a lot, works great!
ReplyDelete#include"sys/socket.h"
ReplyDeleteno such file directory found
how to fix this error
plz tell me