I recently made a server client program in c but it doesn't seem to work. the only thing i get when compiling it is a warning and the fact that there is a fault in the client. Could anyone help me a bit?
This is the server code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int server_socket;
server_socket = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in server_address;
int addrlen = sizeof(server_address);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9000);
server_address.sin_addr.s_addr = INADDR_ANY;
bind (server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
int client_socket;
listen(server_socket, 3);
client_socket=accept(server_socket, NULL, (socklen_t*)&addrlen);
char response[256];
recv(server_socket, &response, sizeof(response), 0);
printf("%s",response);
pclose (server_socket);
return (0);
}
and this is the client code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int network_socket;
network_socket = socket(AF_INET,SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9000);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(network_socket, (struct sockaddr*)&server_address, sizeof(server_address));
if (connection_status == -1) { printf("ERROR"); }
char maw[256] = "this is a message"; send(network_socket, maw, sizeof(maw), 0); close(server_address);
return(0);
pclose(network_socket);
}
There is also an error in the server code.
Server code:
When you want to receive the message from the client, you must use the client_socket in your code in the recv() function. Not the server_socket itself.
Client code:
Why you are using the pclose() function, for closing the socket? It's for pipes and requires a FILE pointer. Furthermore the function never get called (after return statement).
This should work (tested on my system):
Server:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int server_socket;
server_socket = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in server_address;
int addrlen = sizeof(server_address);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9000);
server_address.sin_addr.s_addr = INADDR_ANY;
bind (server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
int client_socket;
listen(server_socket, 3);
client_socket=accept(server_socket, NULL, (socklen_t*)&addrlen);
char response[256];
recv(client_socket, &response, sizeof(response), 0);
printf("%s",response);
shutdown (client_socket, SHUT_RDWR);
close (client_socket);
shutdown (server_socket, SHUT_RDWR);
close (server_socket);
return (0);
}
Client:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int network_socket;
network_socket = socket(AF_INET,SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9000);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(network_socket, (struct sockaddr*)&server_address, sizeof(server_address));
if (connection_status == -1) { printf("ERROR"); }
char maw[256] = "this is a message";
send(network_socket, maw, sizeof(maw), 0);
shutdown (network_socket, SHUT_RDWR);
close(network_socket);
return(0);
}
For the function shutdown() see this answer.
Related
I have been following this tutorial to learn something about socket programming in C. Everythink is working just fine, but it only works on one PC. I would like to know how to host server on one PC (I will use rpi4 in future) and connect to the server with client on another PC.
This is code for server, when client connect to server, it sends "You have reached the server" message to client.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char const *argv[])
{
char server_message[256] = "You have reached the server";
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 0);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
send(client_socket, server_message, sizeof(server_message), 0);
close(server_socket);
return 0;
}
here is client.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char const *argv[])
{
int network_socket = 0;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
if (connection_status == -1)
{
printf("Connection Error!");
return 0;
}
char server_response[256];
recv(network_socket, server_response, sizeof(server_response), 0);
printf("Data from server %s\n", server_response);
close(network_socket);
return 0;
}
I naively tried to replace INADDR_ANY with inet_addr("myip"); but it doesnt work.
Thanks for any help!
(Also sorry my first post on sof, hope everything is ok)
server_address.sin_addr.s_addr = INADDR_ANY;
is wrong, you need to soecify the address of the server, either
by getting the address manually, hardcoded or by some discovery protocol
(lookup gethostbyname()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[]){
// established the socket
char inputBuffer[256] = {};
char message[] = {"Hi this is the server.\n"};
int sockfd = 0;
int forClientSocketfd = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) printf("Fail to create the socket.");
// socket connection
struct sockaddr_in serverInfo, clientInfo;
int addrlen = sizeof(clientInfo);
bzero(&serverInfo, sizeof(serverInfo));
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htron(10024);
bind(sockfd, (struct sockaddr *) &serverInfo, sizeof(serverInfo));
listen(sockfd, 5);
while(1){
forClientSocketfd = accept(sockfd, (struct sockaddr*) &clientInfo, &addrlen);
send(forClientSocketfd, message, sizeof(message), 0);
recv(forClientSocketfd, inputBuffer, sizeof(inputBuffer), 0);
printf("Received from client: %s\n", inputBuffer);
}
return 0;
}
This is the code for socket programming that I seen through from the net. when I compiled it, it throw the error message as below. Having no idea what's going on, even though searching through the internet. p.s. Client operate as normal.
enter image description here
you have a typo on line number 24 it should be htons and not htron
htons()
The htons function takes a 16-bit number in host byte order and returns a 16-bit number in network byte order used in TCP/IP networks(the AF_INET or AF_INET6 address family). The htons function can be used to convert an IP port number in host byte order to the IP port number in network byte order
also add the stdio header file to your code to remove the other warnings
heres the final corrected code with no warnings or errors.
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
// established the socket
char inputBuffer[256] = {};
char message[] = {"Hi this is the server.\n"};
int sockfd = 0;
int forClientSocketfd = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
printf("Fail to create the socket.");
// socket connection
struct sockaddr_in serverInfo, clientInfo;
int addrlen = sizeof(clientInfo);
bzero(&serverInfo, sizeof(serverInfo));
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(10024);
bind(sockfd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
listen(sockfd, 5);
while (1)
{
forClientSocketfd = accept(sockfd, (struct sockaddr *)&clientInfo, &addrlen);
send(forClientSocketfd, message, sizeof(message), 0);
recv(forClientSocketfd, inputBuffer, sizeof(inputBuffer), 0);
printf("Received from client: %s\n", inputBuffer);
}
return 0;
}
I'm trying to pass a command from client to server. However, the client is scanning user input perfectly but the server is just closing without printing anything except for the new line.
Server: BTW the read funciton gives back less than zero so thats where the problem is but can't figure it out.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(){
char server_message[1024] = "You have reached the server";
char recieved[200];
int array[20];
for(int i = 0; i < 20; i++){
array[i] = rand();
}
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 2);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
recv(server_socket, &recieved, sizeof(recieved, 0), NULL);
printf("%s", recieved);
printf("\n");
printf("%s", recieved);
close(server_socket);
return 0;
}
Client: It establishes network perfectly, and sends command to server for print.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(){
int network_socket;
char command[10];
network_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
if (connection_status < 0){
perror("Connection Failed: ");
}
char server_response[256];
printf("Please enter your command:\n");
scanf("%s", command);
int client_socket;
client_socket = accept(network_socket, NULL, NULL);
send(client_socket, command, sizeof(command), 0);
//recv(network_socket, &server_response, sizeof(server_response, 0), NULL);
printf("The Server Sent the data : %s\n",command);
///close(sock);
return 0;
}
I have a very simple client and server code:
Server:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
listen(server_sockfd, 5);
while ( 1 )
{
char ch;
printf("server waiting\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
read(client_sockfd, &ch, 1);
ch += 4;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
Client:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch = 'P';
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(argv[1]);
address.sin_port = htons(9734);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == −1)
{
perror("oops: client1");
return 1;
}
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from server = %c\n", ch);
close(sockfd);
return 0;
}
Consider I have executed the server code in machine A(10.18.17.26), and when I'm trying to execute the client in machine X I'm getting this error:
$ ./client 10.18.17.26
oops: client1: Connection refused
But I'm able to ping machine A from machine X and vice versa. Machine A and X were connected in a intranet network.
But if I run the client code on peer servers of machine A it is working fine and generating this output:
$ ./client 10.18.17.26
char from server = T
Hope I can get a solution here to overcome this. Thanks in advance!
I'm trying to run client server connection. Here's my code:
server.c
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 13
/.../
int main(void)
{
int serverFd, connectionFd;
struct sockaddr_in servaddr;
char timebuffer[MAX_BUFFER+1];
time_t currenTime;
serverFd = socket(AF_INET, SOCK_STREAM, 0);
memset(&servaddr, 0 , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);
bind(serverFd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(serverFd, 5);
while(1)
{
connectionFd = accept(serverFd, (struct sockaddr *) NULL, NULL);
if(connectionFd >= 0)
{
currenTime = time(NULL);
snprintf(timebuffer, MAX_BUFFER, "%s\n", ctime(¤Time));
write(connectionFd, timebuffer, strlen(timebuffer));
close(connectionFd);
}
}
}
client.c
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 13
int main()
{
int connectionFd, in;
struct sockaddr_in servaddr;
char timebuffer[MAX_BUFFER+1];
connectionFd = socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0 ,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(connectionFd,(struct sockaddr_in *) &servaddr , sizeof(servaddr));
while((in = read(connectionFd, timebuffer, MAX_BUFFER)) >0)
{
timebuffer[in] = 0;
printf("\n%s" , timebuffer);
}
/*...*/
close(connectionFd);
}
How to modify server to listen on loopback interface (not other interface)?
Change
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
to just use the loop back address. i.e.
servaddr.sin_addr.s_addr = htonl(2130706433L)
(used http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx to do the maths)