i am very new to socket programming. i am writing a server side and a client side code to send a file over UDP in c. both the codes compile however when i run them on ubuntu bash, the server side code works fine but the client side gives me an error: open: No such file or directory
can anyone please help me out with this!!!
Here is the server side:
/************* UDP SERVER CODE *******************/
#include <fcntl.h>
#include <netdb.h> /* getprotobyname */
#include <sys/stat.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>
int main(){
int udpSocket, nBytes;
char buffer[BUFSIZ];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;
int client_sockfd;
int filefd;
ssize_t read_return;
char *file_path = "output.txt";
/*Create UDP socket*/
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Bind socket with address struct*/
if(bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr))!=0)
printf("Not binded\n");
else
printf("Binded and listening\n");
/*Initialize size variable to be used later on*/
addr_size = sizeof serverStorage;
while (1) {
/*client_addr_size = sizeof(clientAddr);
puts("waiting for client");
client_sockfd = accept(udpSocket,(struct sockaddr*)&clientAddr,&client_addr_size);*/
puts("waiting for client");
nBytes = recvfrom(udpSocket,buffer,BUFSIZ,0,(struct sockaddr *)&serverStorage, &addr_size);
filefd = open(file_path,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR | S_IWUSR);
if (filefd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
do {
read_return = read(nBytes, buffer, BUFSIZ); //read from the client's buffer//
if (read_return == -1) {
perror("read");
exit(EXIT_FAILURE);
}
if (write(filefd, buffer, read_return) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
} while (read_return > 0);
close(filefd);
close(client_sockfd);
}
return EXIT_SUCCESS;
}
Here is the client side:
/************* UDP CLIENT CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#include <netdb.h> /* getprotobyname */
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
int main(){
int clientSocket, portNum, nBytes;
struct sockaddr_in serverAddr;
socklen_t addr_size;
char *file_path = "input.tmp";
int filefd;
ssize_t read_return;
char buffer[BUFSIZ];
char *user_input = NULL;
char *server_reply = NULL;
/*Create UDP socket*/
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Initialize size variable to be used later on*/
addr_size = sizeof serverAddr;
while (1) {
filefd = open(file_path, O_WRONLY | O_APPEND);
if (filefd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
else {
printf("Type a sentence to send to server/file:\n");
fgets(buffer,BUFSIZ,stdin);
write (filefd,buffer,BUFSIZ);
printf("You typed: %s",buffer);
}
read_return = read(filefd, buffer, BUFSIZ);
nBytes = strlen(buffer) + 1;
if (read_return == 0)//indicated end of file
break;
if (read_return == -1) {
perror("read");
exit(EXIT_FAILURE);
}
/*Send message to server*/
sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
/*if (write(clientSocket, buffer, read_return) == -1) {
perror("write");
exit(EXIT_FAILURE);
}else{printf("input file read successfully into the buffer\n");}*/
}
free(user_input);
free(server_reply);
close(filefd);
exit(EXIT_SUCCESS);
}
.. i am very new to socket programming..
filefd = open(file_path, O_WRONLY | O_APPEND);
if (filefd == -1) {
perror("open");
The error open: No such file or directory is unrelated to socket programming. My guess is that file_path (i.e. input.tmp) does not exist. If you want to create a file in case it does not exist you need to add the O_CREAT flag too. See open(2).
open: No such file or directory
That is because the file that you are trying to write doesn't exist (You may want to use O_CREAT).
read: Bad file descriptor
Once you fix the first error, you may also want to fix this second error, which is a result of trying to read from a file that has been opened in write only mode.
Also you may want to write only as many bytes as you have in the buffer (not entire BUFSIZ).
Related
this is program for file transfer server and client
Both server and client code I ran normally. However, when I used the diff command to compare the original file and the transmitted file, only the words 'binary files test.txt and receivefile.txt differ' were output. I know you can compare binary files using cmp, but I have to use diff. How do I change the code so I can compare the two with the diff command?
server code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFFSIZE 4096
#define SERVERPORT 7799
int main(void){
int s_sock, c_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t c_addr_size;
char buf[BUFFSIZE] = {0};
char filename[BUFFSIZE] = {0};
int read_len;
int newfile;
int writefile;
int fileread_len;
s_sock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&server_addr, sizeof(server_addr));
int option = 1;
setsockopt(s_sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
// already in use error
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(s_sock, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1){
perror("bind error");
exit(1);
}
listen(s_sock,1);
c_addr_size = sizeof(struct sockaddr);
c_sock = accept(s_sock, (struct sockaddr *) &client_addr, &c_addr_size);
if(c_sock == -1){
perror("accept error");
exit(1);
}
read_len = read(c_sock, buf, BUFFSIZE);
if(read_len > 0){
strcpy(filename,buf);
}else{
close(c_sock);
printf("file len error");
}
newfile = open(filename, O_RDONLY);
if(!newfile){
perror("there is no file for open");
exit(1);
}
while((fileread_len=read(newfile, buf, BUFFSIZE)) != 0){
writefile = write(c_sock, buf, BUFFSIZE);
}
close(c_sock);
close(newfile);
close(s_sock);
return 0;
}
client code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFFSIZE 4096
#define SERVERPORT 7799
int main(int argc, char* argv[]){
if(argc != 4){
printf("<Usage : ./hw11c IP PORT FILENAME >\n");
}
int c_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t c_addr_size;
char buf[BUFFSIZE] = {0};
char filename[BUFFSIZE] = {0};
int receivefile;
int filenamelen;
int recv_len;
c_sock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&server_addr, sizeof(server_addr));
int option = 1;
setsockopt(c_sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
// already in use
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
strcpy(filename,argv[3]);
filenamelen = strlen(filename);
if(connect(c_sock,(struct sockaddr *)&server_addr, sizeof(server_addr)) == -1){
perror("connect error");
exit(1);
}
send(c_sock, filename, filenamelen, 0);
receivefile = open("receivefile.txt", O_CREAT | O_EXCL | O_WRONLY ,0700);
// receivefile = open(filename, O_CREAT | O_EXCL | O_RDWR);
if(!receivefile){
perror("there is no receive file");
exit(1);
}
while((recv_len = recv(c_sock, buf, BUFFSIZE, 0)) != 0)
{
write(receivefile, buf, recv_len);
}
close(receivefile);
close(c_sock);
return 0;
}
I am trying to check if a specific UDP port is open or not. I am trying to do this by sending UDP packets and checking the ICMP response to see if the UDP port is avaiable or not. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h> /* inet(3) functions */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#define MAXLINE 10096
int main(int argc, char **argv)
{
int sockfd, portno;
struct sockaddr_in servaddr;
struct hostent *server;
int sendfd, recvfd;
server = gethostbyname(argv[1]);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(EXIT_FAILURE);
}
//socket varibles
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&servaddr.sin_addr.s_addr, server->h_length);
//get port from command line arguments
portno = atoi(argv[2]);
servaddr.sin_port = htons(portno);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
// open send UDP socket
if((sendfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
perror("*** socket(,,IPPROTO_UDP) failed ***n");
exit(-1);
}
// open receive ICMP socket
if((recvfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
perror("*** socket(,,IPPROTO_ICMP) failed ***n");
exit(-1);
}
int n;
char sendline[] = "a message"; //string for message to be sent
char recvline[MAXLINE]; //string for message to be received
//send ping request
if(sendto(sendfd, sendline, sizeof(sendline), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
{
perror("*** sendto() failed ***");
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 100000;
if (setsockopt(recvfd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
perror("Error");
}
n = recvfrom(recvfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = '\0'; /* null terminate */
struct iphdr *ip_hdr = (struct iphdr *)recvline;
int iplen = ip_hdr->ihl << 2;
struct icmphdr *icmp = (struct icmphdr *)((char *)ip_hdr + (4 * ip_hdr->ihl));
if((icmp->type == ICMP_UNREACH) && (icmp->code == ICMP_UNREACH_PORT))
{
printf("\nPORT CLOSED\n");
}
else
{
printf("\nPORT OPEN\n");
}
exit(0);
}
How can I get this working? When I run the code, It always says "PORT OPEN" in every port I test it with which definitely cannot be right.
I'm very new in C language and linux and English is not my mother language. Sorry for those in advance.
What I need to is to send .avi or .mp4 files from a client to a server using socket on Linux. I can send the file from client to server but the video on the server is not working.
When I try to play the video, I keep getting an error like "could not determine the type of stream". When I checked the original video on client, the size was 5,787,969 bytes but the size of the video transferred on server is 5,786,954 bytes. I think this is because of data loss when I transfer the file.
How do I fix this?
This is my code below:
Server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // read, write
#include <arpa/inet.h>
#include <sys/types.h> // socket, bind, accept, open
#include <sys/socket.h> // socket, bind, listen, accept
#include <sys/stat.h> // open
#include <fcntl.h> // open
#include <errno.h>
#define PORT 5500
#define MAXBUF 1024
int main() {
int server_sockfd;
int client_sockfd;
int des_fd; // file num
struct sockaddr_in serveraddr, clientaddr;
int client_len, read_len, file_read_len; // length
char buf[MAXBUF];
int check_bind;
client_len = sizeof(clientaddr);
/* socket() */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(server_sockfd == -1) {
perror("socket error : ");
exit(0);
}
/* bind() */
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
if(bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) > 0) {
perror("bind error : ");
exit(0);
}
/* listen */
if(listen(server_sockfd, 5) != 0) {
perror("listen error : ");
}
while(1) {
char file_name[MAXBUF]; // local val
memset(buf, 0x00, MAXBUF);
/* accept() */
client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
printf("New Client Connect : %s\n", inet_ntoa(clientaddr.sin_addr));
/* file name */
read_len = read(client_sockfd, buf, MAXBUF);
if(read_len > 0) {
strcpy(file_name, buf);
printf("%s > %s\n", inet_ntoa(clientaddr.sin_addr), file_name);
} else {
close(client_sockfd);
break;
}
/* create file */
des_fd = open(file_name, O_WRONLY | O_CREAT | O_EXCL, 0700);
if(!des_fd) {
perror("file open error : ");
break;
}
/* file save */
while(1) {
memset(buf, 0x00, MAXBUF);
file_read_len = read(client_sockfd, buf, MAXBUF);
write(des_fd, buf, file_read_len);
if(file_read_len == EOF | file_read_len == 0) {
printf("finish file\n");
break;
}
}
close(client_sockfd);
close(des_fd);
}
close(server_sockfd);
return 0;
}
Client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 5500
#define IP "127.0.0.1"
#define MAXBUF 1024
int main() {
struct sockaddr_in serv_addr;
int s;
int sourse_fd;
char buf[MAXBUF];
int file_name_len, read_len;
/* socket() */
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == -1) {
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(IP);
serv_addr.sin_port = htons(PORT);
if(connect(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
perror("connect : ");
printf("fail to connect.\n");
close(s);
return 1;
}
memset(buf, 0x00, MAXBUF);
printf("write file name to send to the server: ");
scanf("%s", buf);
printf(" > %s\n", buf);
file_name_len = strlen(buf);
send(s, buf, file_name_len, 0);
sourse_fd = open(buf, O_RDONLY);
if(!sourse_fd) {
perror("Error : ");
return 1;
}
while(1) {
memset(buf, 0x00, MAXBUF);
read_len = read(sourse_fd, buf, MAXBUF);
send(s, buf, read_len, 0);
if(read_len == 0) {
break;
}
}
return 0;
}
In Server
Look at your "file save" while loop in Server:
/* file save */
while(1) {
memset(buf, 0x00, MAXBUF);
file_read_len = read(client_sockfd, buf, MAXBUF);
write(des_fd, buf, file_read_len);
if(file_read_len == EOF | file_read_len == 0) {
printf("finish file\n");
break;
}
}
Instead of file_read_len == EOF | file_read_len == 0 you should use only file_read_len == 0.
Still in Server, inside open() function that creates the file, I also recommend to change 0700 to S_IRWXU & (~S_IXUSR) __but_it_is_not_mandatory__. See user#host:~$ man 2 open for more details.
In Client
In Client you only need to add "+ 1" in strlen(buf):
file_name_len = strlen(buf) + 1;
That is because a string needs to end with a null byte. And strlen() returns the length of string minus the null byte at the end :-)
I also recommend you to use argc and argv to get arguments, look:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main (int argc, char **argv) {
int how_many_files = argc - 1; /* '- 1' cause first arg is program name
*/
int fd; /* file descriptor */
int i; /* a counter */
char buffer[10]; /* a buffer */
for (i = 0; i < how_many_files; i++) {
fd = open(argv[i + 1], O_RDONLY);
if (fd == -1)
continue;
read(fd, buffer, 10);
buffer[9] = '\0'; /* last byte of buffer needs to be null char*/
printf("First 10 Bytes of the file is: %s\n",
buffer);
close(fd);
}
return 0;
}
Please help! This is due in a few hours and I've had no luck w/ online searches
CODE:
/*
Description:
The client should first send a datagram to the LocationServer.
The LocationServer will be listening on port number 23510 of the host c-lnx001.engr.uiowa.edu.
The message contained in this datagram should be the specified UserID (no null characters, blanks, line-feeds, or other extraneous characters).
The LocationServer will respond with a datagram containing the following information:
[WeatherServer hostname] [WeatherServer port #]
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h> /* for fprintf */
#include <string.h> /* for memcpy */
#include <strings.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#define SIZE 2048
int main(){
struct hostent *hp; /* host information */
struct sockaddr_in servaddr; /* server address */
char *my_message = "laura\0";/*USERID as message to server*/
char *buf_addr;
char *host = "c-lnx001.engr.uiowa.edu\0";
int port = 23510;
int fd;
/* fill in the server's address and data */
//memset((char*)&servaddr, 0, sizeof(servaddr)); ?
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(23510);
/* look up the address of the server given its name */
hp = gethostbyname("c-lnx001.engr.uiowa.edu");
if (!hp) {
fprintf(stderr, "could not obtain address of %s\n", host);
return 0;
}
/* put the host's address into the server address structure */
// DOESN't WORK: memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
bcopy(hp->h_addr,(char*)&servaddr.sin_addr,hp->h_length);
if((fd = socket(AF_INET,SOCK_DGRAM, 0))<0)
{
exit(2);
}
/* send a message to the server */
if (sendto(fd, my_message, strlen(my_message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("sendto failed");
return 0;
}
int addrlen = sizeof(servaddr);
recvfrom(fd,buf_addr,SIZE,0,(struct sockaddr*)&servaddr,&addrlen);
printf("%s\n", buf_addr);
}
I think your problem is that you're not resetting the serveraddr
Here's a working copy
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define IP_ADDRESS "127.0.0.1"
#define SERVER_PORT 5060
int main (int argc, char *argv[])
{
int recBytes, clientSocket=socket(AF_INET, SOCK_DGRAM, 0);
char buf[80], msg[]="greeting to server";
const int msgLen = sizeof(msg);
struct sockaddr_in serverAddr;
socklen_t reSize;
memset(&serverAddr, sizeof(struct sockaddr_in), 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
inet_aton(IP_ADDRESS, &serverAddr.sin_addr);
if(sendto(clientSocket, msg, sizeof(msg), 0, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != (sizeof(msg)))
{
perror("sendto");
return -1;
}
reSize = sizeof(struct sockaddr);
if((recBytes = recvfrom(clientSocket, buf, sizeof(buf)-1, 0, (struct sockaddr*)&serverAddr, &reSize)) == -1)
{
perror("recvfrom");
return -1;
}
buf[recBytes]='\0';
printf("%s\n", buf);
return 0;
}
I have a simple server and a client. I run the server at some port in my machine and when I try to connect my client to the server, it says network is unreachable. Can someone please suggest me why is it not being able to connect to the server. Please have a look at the files below:
server.c
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[]){
int sockfd, newsockfd, portno;
struct sockaddr_in serv_addr;
char sendmessage[50];
if(argc != 2){
fprintf(stderr, "ERROR, Port number not provided or Command line argument is not 2\n");
exit(1);
}
//creating a socket for the server
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
error("ERROR opening socket");
}
portno = atoi(argv[1]);
//describing the attributes for socket address
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0){
error("Error on binding the socket");
exit(1);
}
//allowing only 1 client to connect to the server at a time
if(listen(sockfd, 1) < 0){
error("Error in listening to the socket");
}
printf("Server is running...... \nWaiting for the connection from the client on port: %d\n", portno);
while(1){
//accepts the connection from the client
newsockfd = accept(sockfd, (struct sockaddr*)NULL, NULL);
if(newsockfd < 0){
error("Error on accepting");
}
strcpy(sendmessage, "Welcome to The Server");
write(newsockfd, sendmessage, strlen(sendmessage));
}
return 0;
}
client.c
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char* argv[]){
int sockfd;
char recvmessage[100];
char sendmessage[100];
int portno;
struct hostent *server;
struct sockaddr_in serv_addr;
if(argc != 3){
fprintf(stderr, "Error, either IP address or port number not provided.\n");
exit(1);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(socket < 0){
error("Error with creating a socket");
}
//check whether the host exist or not
server = gethostbyname(argv[1]);
if(server == NULL){
fprintf(stderr, "ERROR, the host is not defined\n");
exit(0);
}
//creating the socket
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
//connecting the client to the socket
if(connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0){
error("Could not connect to the server......");
exit(1);
}
printf("Connection Successful to the Server\n");
return 0;
}
First of all make sure you pass the same port number to both server & client. If the port number is different, communication between server and client won't happen.
Here is the code for local machine. You can change the code a little and pass IP addresses.
Server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORTNUM 2348
#define bufferLength 500
int main(int argc, char *argv[])
{
char buffer[bufferLength];
struct sockaddr_in dest; /* socket info about the machine connecting to us */
struct sockaddr_in serv; /* socket info about our server */
int mysocket; /* socket used to listen for incoming connections */
socklen_t socksize = sizeof(struct sockaddr_in);
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
serv.sin_port = htons(PORTNUM); /* set the server port number */
mysocket = socket(AF_INET, SOCK_STREAM, 0);
/* bind serv information to mysocket */
bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
/* start listening, allowing a queue of up to 1 pending connection */
listen(mysocket, 1);
int consocket;
int cpid;
while(1)
{
consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
perror("consocket\n");
if( (cpid = fork()) == 0 )
{
printf("inside child process\n\n\n");
close(mysocket);
close(consocket);
int recivedBytes = recv(consocket, buffer, bufferLength, 0);
buffer[recivedBytes] = '\0';
printf("recieved data %s \n", buffer);
return 0;
}
else
close(consocket);
}
close(mysocket);
return EXIT_SUCCESS;
}
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define MAXRCVLEN 500
#define PORTNUM 2348
int main(int argc, char *argv[])
{
char buffer[] = "My name is khan"; /* +1 so we can add null terminator */
int len, mysocket;
struct sockaddr_in dest;
mysocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&dest, 0, sizeof(dest)); /* zero the struct */
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("127.0.0.1"); /* set destination IP number */
dest.sin_port = htons(PORTNUM); /* set destination port number */
connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
len = send(mysocket, buffer, strlen(buffer), 0);
perror("len\n");
/* We have to null terminate the received data ourselves */
buffer[len] = '\0';
printf("sent %s (%d bytes).\n", buffer, len);
close(mysocket);
return EXIT_SUCCESS;
}
Hope this helps