C Socket, server and client. connect error: "No route to Host" - c

I've written the client code and the server, however, there is a problem with connect: "No route to host". It is strange because clients and servers operate in a different network. Also, if I ping [my ip] tells me timeout, if I telnet [my ip] tells me error. Can you advise me how to solve this problem? I opened port 2222 from the router but it still does not seem to work.
Sorry for my English
Client code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#define ADDRESS "[IP]"
#define PORT 2222
int main()
{
int sockid;
struct sockaddr_in c_address;
printf("[CLIENT] Eseguito.\n");
c_address.sin_family = AF_INET;
c_address.sin_addr.s_addr = inet_addr(ADDRESS);
c_address.sin_port = htons(PORT);
sockid = socket(AF_INET, SOCK_STREAM, 0);
int sock_conn;
sock_conn = connect(sockid, (struct sockaddr *)&c_address, sizeof(c_address));
if(sock_conn == -1)
{
perror("Connect client");
exit(EXIT_FAILURE);
}
while(1)
{
char text[128];
printf("[CLIENT] Scrivi qualcosa: ");
fgets(text, 128, stdin);
write(sockid, text, 128);
read(sockid, text, 128);
printf("[CLIENT] Testo ricevuto: %s.\n", text);
}
}
Server code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#define PORT 2222
void toggleCharacter(char *str);
int main()
{
int sockid;
struct sockaddr_in s_address;
printf("[SERVER] Eseguito.\n");
s_address.sin_family = AF_INET;
s_address.sin_addr.s_addr = htonl(INADDR_ANY);
s_address.sin_port = htons(PORT);
sockid = socket(AF_INET, SOCK_STREAM, 0);
if(bind(sockid, (struct sockaddr *)&s_address, sizeof(s_address)) == -1)
{
perror("Bind server");
exit(EXIT_FAILURE);
}
if(listen(sockid, 5) == -1)
{
perror("Listen server");
exit(EXIT_FAILURE);
}
int connection_socket;
connection_socket = accept(sockid, NULL, 0);
if(connection_socket == -1)
{
perror("Accept server");
exit(EXIT_FAILURE);
}
char text[128];
while(1)
{
read(connection_socket, text, 128);
printf("[SERVER] Messaggio ricevuto: %s.\n", text);
printf("[SERVER] Scrivi qualcosa: ");
fgets(text, 128, stdin);
write(connection_socket, text, 128);
}
close(connection_socket);
}

Related

Can't port Linux TCP socket program in C to Windows

I'm trying to import C programs, which demonstrate TCP socket programming in the Linux environment, into Windows. In the following two programs, the commented out include statements work in Linux and the includes below them are the Windows equivalent that I put. However, when I'm running these two programs, the server and the client produce the following errors respectively.
Error from simple_server
Socket received successfully...Failed to listen...
Error from simple_client
Read Error
All the equivalent Windows includes were taken from a long Stackoverflow searching session itself..:D. However, I'm stuck here not understanding why my server is not running. Any helpful advice and suggestions are highly appreciated.
My code
simple_server.c
// #include <arpa/inet.h>
// #include <sys/socket.h>
// #include <netinet/in.h>
#include <Winsock2.h>
#include <stdio.h>
#include <stdlib.h>
// #include <unistd.h>
#include <io.h>
// Windows equivalent to sleep() in unistd.h
#include <windows.h>
#define sleep(n) Sleep(n*1000)
#define usleep(n) Sleep(n/1000)
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main (void){
int listenfd, connfd = 0;
struct sockaddr_in serv_addr;
char send_buff[1024];
int numrv;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
printf("Socket received successfully...");
memset(&serv_addr, '0', sizeof(serv_addr));
memset(send_buff, '0', sizeof(send_buff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);
serv_addr.sin_port = htons(50001);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(listen(listenfd,10) == -1){
printf("Failed to listen..");
return -1;
}
while(1){
connfd = accept(listenfd,(struct sockaddr*)NULL, NULL);
strcpy(send_buff, "Message from the server");
write(connfd, send_buff, strlen(send_buff));
close(connfd);
sleep(1);
}
return 0;
}
simple_client.c
#include <sys/types.h>
// #include <arpa/inet.h>
// #include <sys/socket.h>
// #include <netinet/in.h>
// #include <netdb.h>
#include <Winsock2.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// #include <unistd.h>
#include <io.h>
#include <errno.h>
int main(void){
int sockfd, n =0;
char receive_buffer[1024];
struct sockaddr_in serv_addr;
memset(receive_buffer, '0', sizeof(receive_buffer));
if((sockfd == socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("Error: couldn't receive socket.");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0){
}
while ((n = read(sockfd, receive_buffer, sizeof(receive_buffer)-1)) > 0){
receive_buffer[n] = 0;
if(fputs(receive_buffer,stdout) == EOF){
printf("\nError: fputs error");
}
printf("\n");
}
if(n<0){
printf("\n Read Error");
}
return 0;
}

gethostbyname on remote client but getting sometimes 'Resource temporarily unavailable'

sometimes, expecially when client and server are on different machines (one in my ubuntu and the other one in a virtual-machined ubuntu), the client which tries to connect to the server via hostname gets a
Error getHostEntry: Resource temporarily unavailable.
This is the server.c file:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <malloc.h>
#include <netdb.h>
void checkHostname(int);
void checkHostEntry(struct hostent*);
void checkIpBuffer(char*);
int main(int argc, char **argv) {
struct sockaddr_in socketAddress;
int socketDescriptor, clientDescriptor, nread;
char buffer[1000];
pthread_t threadId;
char hostbuffer[256];
char *IPbuffer;
struct hostent *hostEntry;
int hostname;
if((socketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("Errore durante la creazione della socket\n");
exit(1);
}
socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
socketAddress.sin_port = htons(atoi(argv[1]));
if(bind(socketDescriptor, (struct sockaddr*) &socketAddress, sizeof(socketAddress)) != 0) {
printf("Errore di bind()\n");
close(socketDescriptor);
exit(1);
}
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
checkHostname(hostname);
hostEntry = gethostbyname(hostbuffer);
checkHostEntry(hostEntry);
IPbuffer = inet_ntoa(*((struct in_addr*) hostEntry->h_addr_list[0]));
printf("Hostname - %s\n", hostbuffer);
printf("Host IP - %s:%d\n", IPbuffer, ntohs(socketAddress.sin_port));
if(listen(socketDescriptor, 100) < 0) {
printf("Errore di listen()\n");
close(socketDescriptor);
exit(1);
}
return 0;
}
void checkHostname(int hostname) {
if (hostname == -1) {
perror("Errore gethostname");
exit(1);
}
}
void checkHostEntry(struct hostent *hostentry) {
if (hostentry == NULL) {
perror("Errore getHostEntry");
exit(1);
}
}
void checkIpBuffer(char *ip) {
if (ip == NULL) {
perror("Errore inet_ntoa");
exit(1);
}
}
And this is my client.c file
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <netdb.h>
int main(int argc, char **argv) {
struct sockaddr_in clientAddress;
int choice, nread, serverIp;
char *IPbuffer;
struct hostent *host_entry;
if((clientDescriptor = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("Errore nella creaione del socket\n");
exit(1);
}
host_entry = gethostbyname(argv[1]);
checkHostEntry(host_entry)
IPbuffer = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = inet_addr(IPbuffer); // indirizzo IP
clientAddress.sin_port = htons(atoi(argv[2])); // porta
if(connect(clientDescriptor, (struct sockaddr*) &clientAddress, sizeof(clientAddress)) < 0) {
printf("Errore connect()\n");
close(clientDescriptor);
exit(1);
}
return 0;
}
void checkHostEntry(struct hostent *hostentry) {
if (hostentry == NULL) {
perror("Errore getHostEntry");
exit(1);
}
}
If I
./server.out 8888
it displays:
IP: 1.2.3.4
Hostname: johnmayer
and If i try to connect from another computer (with a ubuntu installed on a virtual box) to this server, using
./client johnmayer 8888
I sometimes get
Error getHostEntry: Resource temporarily unavailable.
Is there something wrong in the code?

Connect() function in C, invalid argument

I'm coding a client/server, the client simply sends a message to the server that he will print the message.
To do this I used sockets and localhost. Here is the code:
server:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include"thpool.h"
#include"functions.h"
#define N 7
#define SERVER_PATH "/tmp/server"
int main(void){
unlink(SERVER_PATH);
struct sockaddr_un stru;
int sock_serv, new_sock;
int opt = 1;
struct sockaddr* cliaddr;
char buff[N];
cliaddr = malloc(sizeof(struct sockaddr));
socklen_t addrlen = strlen((char* )cliaddr);
if((sock_serv = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
printf("socket creation error");
exit(-1);
}
bzero(&stru, sizeof(struct sockaddr_in));
stru.sun_family = AF_UNIX ;
strncpy (stru.sun_path, SERVER_PATH, sizeof(stru.sun_path));
if((bind(sock_serv, (struct sockaddr*) &stru , sizeof(struct sockaddr_un ))) < 0){
perror("bind failed");
exit(EXIT_FAILURE);
}
if(listen(sock_serv, SOMAXCONN) < 0){
perror("listen error\n");
exit(EXIT_FAILURE);
}
if((new_sock = accept(sock_serv, NULL, 0)) < 0){
perror("accept error\n");
exit(EXIT_FAILURE);
}
read(new_sock , buff, N) ;
printf("Server got: %s\n" , buff);
close(sock_serv);
close(new_sock);
unlink(SERVER_PATH);
return 0;
}
and here is the client:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include<errno.h>
#include"thpool.h"
#include"functions.h"
#define SERVER_PATH "/tmp/server"
int main(void){
int sock_cl;
struct sockaddr* sa;
socklen_t sa_lenght;
sa = malloc(sizeof(struct sockaddr));
sa_lenght = strlen((char* )sa);
if((sock_cl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket creation error");
exit(EXIT_FAILURE);
}
sa->sa_family = AF_UNIX ;
strncpy (sa->sa_data, SERVER_PATH, sizeof(sa->sa_data));
while (connect(sock_cl , (struct sockaddr*)&sa , (socklen_t)sa_lenght) == -1) {
perror("connection to the server failed");
exit(EXIT_FAILURE);
}
write (sock_cl, "Hello!", 7);
printf("message sended\n");
close(sock_cl);
return 0;
}
I have a problem with the connect() function, the error is "invalid argument". Note that I first executed the server and then the client, so is not that the problem.
This is how you define sa, as a pointer to struct sockaddr.
struct sockaddr* sa;
Here you take the address of the variable sa and cast it to the type of sa.
(struct sockaddr*)&sa
The result is a pointer to pointer to struct sockaddr, which gets brute force cast to pointer to struct sockaddr.
Type-casting is a trap and you got caught in it.
To solve, I recommend using a tutorial on sockets.
I think that when comparing your client and the example client in this tutorial especially the pointer level issue you have created becomes nicely visible:
https://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html
Your server and client are both misusing the sockaddr... structures. The error on the server side doesn't affect anything because it is not actually using the faulty sockaddr it allocates, it just leaks. But your client is completely misusing the sockaddr that is passed to connect(), which is why connect() fails.
Try this instead:
server
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include "thpool.h"
#include "functions.h"
#define N 7
#define SERVER_PATH "/tmp/server"
int main(void){
unlink(SERVER_PATH);
struct sockaddr_un stru;
int sock_serv, new_sock;
ssize_t bufflen;
char buff[N];
if((sock_serv = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
printf("socket creation error");
exit(-1);
}
bzero(&stru, sizeof(stru));
stru.sun_family = AF_UNIX;
strncpy (stru.sun_path, SERVER_PATH, sizeof(stru.sun_path));
if((bind(sock_serv, (struct sockaddr*) &stru, sizeof(stru))) < 0){
perror("bind error");
exit(EXIT_FAILURE);
}
if(listen(sock_serv, SOMAXCONN) < 0){
perror("listen error");
exit(EXIT_FAILURE);
}
if((new_sock = accept(sock_serv, NULL, 0)) < 0){
perror("accept error");
exit(EXIT_FAILURE);
}
bufflen = read(new_sock, buff, N);
if (bufflen < 0) {
perror("read error");
}
else if (bufflen == 0) {
printf("Client disconnected\n");
}
else {
printf("Server got: %.*s\n", (int) bufflen, buff);
}
close(new_sock);
close(sock_serv);
unlink(SERVER_PATH);
return 0;
}
client
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <errno.h>
#include "thpool.h"
#include "functions.h"
#define SERVER_PATH "/tmp/server"
const char *msg = "Hello!";
int main(void){
int sock_cl;
struct sockaddr_un sa;
ssize_t sent;
if((sock_cl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket creation error");
exit(EXIT_FAILURE);
}
bzero(&s, sizeof(sa));
sa.sa_family = AF_UNIX;
strncpy (sa.sa_data, SERVER_PATH, sizeof(sa.sa_data));
if (connect(sock_cl, (struct sockaddr*) &sa, (socklen_t) sizeof(sa)) < 0) {
perror("connect error");
exit(EXIT_FAILURE);
}
sent = write(sock_cl, msg, strlen(msg)+1);
if (sent < 0) {
perror("write error");
}
else {
printf("Message sent: %.*s\n", (int) sent, msg);
}
close(sock_cl);
return 0;
}

C / Linux - How to connect and read the answer of a mail server like gmail or outlook with sockets?

I would like to connect to a mail server to read the mail. For example, I have an outlook address toto#outlook.com, and I want to get all the mail of this mailbox and be able to read it. So I believe I only have to do a client, but : How do I contact and connect to this type of servers, and how do I listen the answer?
Here is my current client code :
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int main()
{
struct protoent *pe;
int fd_client;
struct sockaddr_in s_in;
int port;
char *ip;
char buff[4096];
ip = strdup("157.56.242.251"); // I get it while calling "ping outlook.com"
port = 210;
pe = getprotobyname("TCP");
fd_client = socket(AF_INET, SOCK_STREAM, pe->p_proto);
s_in.sin_family = AF_INET;
s_in.sin_port = htons(port);
s_in.sin_addr.s_addr = inet_addr(ip);
if (connect(fd_client, (struct sockaddr *)&s_in, sizeof(s_in)) == -1)
{
if (close(fd_client) == -1)
return (-1);
fprintf(stderr, "Failed to connect\n");
return (-1);
}
//From here I don't know what to do, so I tried this
recv(fd_client, buff, 4096, 0);
printf("%s\n",buff);
write(fd_client, "Hello !\n", strlen("Hello !\n"));
//To Here
if (close(fd_client) == -1)
return (-1);
return (0);
}
Thanks in advance.

access data from server udp

I have a problem, I have a client server udp, in server side I need to access data from client side to control movement of the robot. For the experiment I use to print "oke" if the value of the data is 1.
here the code program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MYPORT 4950
#define MAXBUFLEN 100
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
struct hostent *he;
int addr_len, numbytes;
char dt[30];
char buf[MAXBUFLEN];
int main()
{
printf("‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐\n");
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
perror("socket");
exit(1); }
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero),'\0',8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1); }
while(1){
addr_len = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&addr_len))==-1)
{
perror("recvfrom");
exit(1);}
buf[numbytes]='\0';
printf("%s : \"%s\"\n", inet_ntoa(their_addr.sin_addr), buf);
if (buf[0]==1)
{ printf("oke\n");}
printf("Me : ");
scanf("%s", dt);
if((numbytes=sendto(sockfd,dt,strlen(dt),0,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)))==-1)
{
perror("sendto");
exit(1);
}
}
close(sockfd);
return 0;
}
when I put "1" in client side the result is:
‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐
130.130.66.76 : "1"
Me :
even in the program there are:
if (buf[0]==1)
{ printf("oke\n");}
why the program cannot access to inside of if?
Try if (buf[0]=='1')
Number characters map to different ASCII values than their number, so, for example, '1'==49.

Resources