Connection refused error in client and server program - c

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!

Related

Connection between two computers using sockets in C. (TCP)

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()

Client server program in c message question

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.

Socket Programming in C, server code with an error

#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;
}

sockets programming: sending and receiving different data to different clients in C

I have written a basic client server code in c socket programming using the TCP/IP protocol but i cant figure out how to make it connect to different clients and send/receive different data to and from them as a function to the client (meaning if its the first client send him that data and if its that client send him the other data) and so on.
This is the only results i have found were sending the same data to different clients.
Current Server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
int main() {
char server_message[100] = {0};
int server_socket = 0;
int client_socket = 0;
struct sockaddr_in server_address;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
// bind the socket to our specified IP and port
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 2);
client_socket = accept(server_socket, NULL, NULL);
printf("Please enter a massage:");
fgets(server_message, 100, stdin);
send(client_socket, server_message, sizeof(server_message), 0);
close(server_socket);
return 0;
}
By using original code from geeksforgeeks and Myst comment we can solve it.
You have one server that serves on local host 127.0.0.1, and can have multiple clients for this example i assume 5 clients are enough.
Run server once, and run many client to connect seprately to that server.
Server.c
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
#define STRING_SIZE 100
#define BUFFER_SIZE 100
int main(int argc, char const *argv[])
{
int server_fd, new_socket[5], valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
for (int i=0;i<5;i++){
if ((new_socket[i] = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket[i], buffer, 1024);
printf("%s\n", buffer);
char send_buf[STRING_SIZE] = "hello";
char buf[BUFFER_SIZE]={0};
sprintf(buf, "%d", i);
strcat(send_buf, buf);
send(new_socket[i], send_buf, strlen(send_buf), 0);
//printf("Hello message sent\n");
}
return 0;
}
Client.c
// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
//printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n", buffer);
return 0;
}
Run
After compiling codes with gcc client.c -o client and gcc server.c -o server
Open one terminal for server and start server by run ./server.
Now you can connect many client [up to 5] to it by running ./client.

Server not receiving information from client?

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;
}

Resources