C programming TCP server and client connection errors - c

I want to make a TCP connection between my Virtual private server and my host machine using a TCP socket connection in C programming.
The serverside code is good and runs flawlessly.
Its the client side that only returns the string that the server is supposed to send out on the FIRST attempt of running it. After that the code doesnt work anymore and i have to restart my terminal and recompile the code for it to work again.
am i doing it right? did i call the IP of my vps right in my client.c?
This is my host machines client.c code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
// create a socket
int mySocket;
mySocket = socket(AF_INET, SOCK_STREAM, 0);
//specify an address structure for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(666);
server_address.sin_addr.s_addr = inet_addr("IP OF MY VPS");
int connection_status = connect(mySocket, (struct sockaddr *) &server_address, sizeof(server_address));
//check for error with the connection
if (connection_status == -1) {
printf("There was an error making a connection to the remote socket \n\n");
exit(1);
}
// recieve data from the server
char server_response[256];
recv(mySocket, &server_response, sizeof(server_response), 0);
// pritn out the server's response
printf("The server sent the data: %s\n \n",server_response);
close(mySocket);
return 0;
}
Now here is the code for my VPS's server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
int main()
{
char server_message[256] = "client has connected";
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(666);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (stuct sockaddr*) &server_address,
sizeof(server_address));
listen(server_socekt, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
send(client_socket, server_message,sizeof(server_message), 0);
close(server_socket);
return 0;
}
note: this code works some times but then most of the time it doesnt

You have no processing loop in the server: each time a client connects, after sending it a message, the server stops listening and terminates.
You can correct the problem in the server:
/* listen for new clients */
listen(server_socket, 5);
while (1)
{
int client_socket;
/* wait for a new client */
client_socket = accept(server_socket, NULL, NULL);
/* send the message */
send(client_socket, server_message,sizeof(server_message), 0);
/* and close only the client socket, not the listening one*/
close(client_socket);
}
/* Once the while loop is finished, you can stop listen (up to you to
change the while loop condition)*/
close(server_socket);
Another thing: you should use perror function to display errors messages, for instance,
int connection_status = connect(mySocket, (struct sockaddr *) &server_address, sizeof(server_address));
//check for error with the connection
if (connection_status == -1) {
perror("connect");
exit(1);
}
will give you this kind of message on error:
connect: Connection refused

Related

How to setup DGRAM socket in C

My server connects successfully, but I am not seeing any message from client/server in my terminal whenever I use SOCK_DGRAM. Am I missing any functions for this type of socket? The code below works fine with SOCK_STREAM with this I also have one small problem, I am not seeing message from client in my server terminal but I do see "Connected to server" in client terminal. Can someone advise?
Client
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
int main() {
int socket_server;
int socket_connect;
int socket_listen;
char buffer[256];
char sendMsg[256] = "Received from client";
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(4003);
socket_address.sin_addr.s_addr = INADDR_ANY;
socket_server = socket(AF_INET, SOCK_DGRAM, 0);
socket_connect= connect(socket_server, (struct sockaddr*) &socket_address, sizeof(socket_address));
socket_listen = listen(socket_server, 5);
recv(socket_server, &buffer, sizeof(buffer), 0);
send(socket_server, &sendMsg, sizeof(sendMsg), 0);
printf("%s", buffer);
return 0;
}
Server
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
int main() {
int socket_server;
int socket_bind;
int socket_listen;
char buffer[256] = "Connected to server";
char fromClient[256];
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(4003);
socket_address.sin_addr.s_addr = INADDR_ANY;
socket_server = socket(AF_INET, SOCK_DGRAM, 0);
socket_bind = bind(socket_server, (struct sockaddr*) &socket_address, sizeof(socket_address));
socket_listen = listen(socket_server, 5);
for(;;) {
int socket_accept = accept(socket_server, NULL, NULL);
send(socket_accept, &buffer, sizeof(buffer), 0);
recv(socket_accept, &fromClient, sizeof(fromClient), 0);
printf("%s", fromClient);
}
return 0;
}
UDP sockets don't use listen or accept.
Messages from all clients are received on the original socket to which you bound the address. The connect is not really establishing a connection, it is just saving the remote address so that it knows where to send each datagram when using send (as opposed to sendto).
It also looks odd that the client expects the first message to come from the server, probably because you expected the server to detect the connect, which it does not. Normally the server would be up and running and send responses to requests, so the client needs to send a request and then wait for a response, and the server needs to wait for a request and then send a response. The server will need to use recvfrom so that it knows where to send the response!

I need help understanding the bind function in C socket programming

I'm trying to really understand sockets, so I am learning it in C. 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() {
char server_message[255] = "You have reached the server!";
//create server socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//define server addr
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 socket to IP and Port
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
//send message
send(client_socket, server_message, sizeof(server_message), 0);
//close socket
close(server_socket);
return 0;
}
In the bind function, this part is needed to specify the address
(struct sockaddr*) &server_address
I read the man page, and it does say it has to be this way, the code also runs, but I don't understand this syntax. What is happening in this part of the code. I know * is used to declare pointers, and & refers to the memory address of the variable. I just can't put it all together.

use sockets to read an RFID device from the network using C

I found an RFID reader with a finger print reader that I would like to use. I tried to use sockets to listen to the port, but I'm unsure on how can I do it properly. The device does not have any labels, so I cannot find data from manufacturer on how to establish the connection. The devices works as I have tested with another software used for clocking in, so the device has now an IP address assigned that I'm able to ping. I have tried a socket server program to listen to the port, but I'm not getting anything when I read the RFID tags. How can I read data from the device when it is connected to the network and store the data?
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
main(){
char server_message[256] = "You have reached the server";
//create the server socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//define the server address
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 the socket to our specified IP and port
bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket;
client_socket = accept(server_socket,NULL,NULL);
char server_response[256];
recv(server_socket, &server_response, sizeof(server_response), 0);
//print out server's response
printf("The server sent the data: %s\n", server_response);
//send the message
send(client_socket, server_message, sizeof(server_message), 0);
//close the socket
close(server_socket);
return 0;
}

Can only connect to Server once with Client in c

I'm writing in C and creating a Server that receives connections from multiple different Clients one at a time. However, after the first connection closes, the server can't be connected to again.
server.c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
int main() {
while(1) {
char server_message[256] = "You have reached the server!";
//Create a socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//Define the server address
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 the socket to the IP and port
bind(server_socket, (struct sockaddr *) &server_address,
sizeof(server_address));
//Listen for connections
listen(server_socket, 5);
//Accept the connection
int client_socket = accept(server_socket, NULL, NULL);
//Send message
send(client_socket, server_message, sizeof(server_message), 0);
//Close the socket
close(server_socket);
}
return 0;
}
You're looping creating a server socket, accepting one connection, processing it, and then closing your server socket. The last action will throw away all pending connections. Your loop should start immediately after the listen() call and terminate after closing the client socket, and before closing the server socket.
You are also ignoring all errors on socket(), bind(), listen(), accept(), send(), and close(). Don't do that.
Change the code like this(omitted error checking):
int main() {
//Create a socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//Define the server address
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 the socket to the IP and port
bind(server_socket, (struct sockaddr *) &server_address,
sizeof(server_address));
//Listen for connections
listen(server_socket, 5);
while(1) {
char server_message[256] = "You have reached the server!";
//Accept the connection
int client_socket = accept(server_socket, NULL, NULL);
/*check some condition to exit from loop*/
/*or send client to another thread for parallel programing*/
//Send message
send(client_socket, server_message, sizeof(server_message), 0);
//Close the client socket
close(client_socket);
}
close(server_socket);
return 0;
}
in this code server after get client must wait the client finished it's job and then try to accept new client, if you want parallel, accept client and data transfer in client, it's better to put client send and receive in another thread.

How to establish connection between two machines using sockets

I want to establish connection between two virtual machines using sockets programmed in C. Client socket is on Kali linux virtual machine and server socket is on Windows Server 2008 R2 VM. Network adapters on virtual machines are set to NAT. Server's IP address is 192.168.1.5 and client's IP address is 192.168.1.3 and there is connection, I have ping between them. Sockets work fine when both the client and server are on linux machine. But when I create and start server socket on server machine, I don't have connection between them and I can't get the message from server on the client socket. Can anyone help, please?? There is my code for sockets:
Server socket:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main() {
char server_message[256] = "YOU HAVE REACHED THE SERVER";
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//DEFINE THE SERVER ADDRESS
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = inet_addr("192.168.1.3");
//BIND SOCKET TO OUR SPECIFIED IP AND PORT
bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
//SEND THE MESSAGE
send(client_socket, server_message,sizeof(server_message),0);
//close the socket
close(server_socket);
return 0;
}
Client socket:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main() {
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
//specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = inet_addr("192.168.1.5");
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);
printf("THE SERVER SENT THE DATA: %s \n", server_response);
close(network_socket);
return 0;
}

Resources