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

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

Related

Why isn't this c client able to read data sent by this c server if the latter calls the write system call twice?

How come this client is only able to read data sent from the first server write system call ? It correctly reads the data sent by the first write, but not with the second one...
Here's the client :
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 1025
#define SA struct sockaddr
int main(){
int sockfd;
struct sockaddr_in servaddr, cli;
//CREATE SOCKET
sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
char buff[1024];
while( (read(sockfd, buff,sizeof(buff))) > 0){
printf("%s\n",buff);
}
close(sockfd);
return 0;
}
And here is the server :
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SA struct sockaddr
#define PORT 1025
int main(){
int sockfd, connfd;
unsigned int len;
struct sockaddr_in servaddr, cli;
//SOCKET CREATION
sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr =htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
//BIND
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
//LISTEN
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
for(;;){
len = sizeof(cli);
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
}
char buff[]="Hi";
char buff2[]="More data sent..\n";
write(connfd,buff,sizeof(buff)); //work
write(connfd,buff2,sizeof(buff2)); //doesn't work
close(connfd);
}
return 0;
}
I also noticed it works if I put the second write inside a loop that requires a certain amount of time. For example :
long max=0;
while(max<1000000000){
//second write works
write(......);
}
Could someone give an explanation of why write behaves like that and what's going on under the hood ?

Why bind function is returning -1

So i am following the book of UNIX network programming, and tried to write a simple daytime server from chapter 1 and client but the bind function is always returning an error, what I'm doing wrong can anyone help??
server.c
/*
* Daytime Server
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
void printError(char *str)
{
printf("%s", str);
exit(0);
}
int main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
time_t ticks;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printError("Error at line 32 socket fuct.");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printError("Error at bind function");
}
if (listen(listenfd, 1024) < 0)
{
printError("Error at listen fuct.");
}
while (1)
{
if ((connfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) < 0)
{
printError("Error at accept fuct.");
}
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
if (write(connfd, buff, strlen(buff)) < 0)
{
printError("Error at write fuct.");
}
close(connfd);
}
return EXIT_SUCCESS;
}
client.c
/*
* Daytime Client
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
void printError(char *str)
{
printf("%s", str);
exit(0);
}
int main(int argc, char **argv)
{
int socketfd, n;
char recvline[4097];
struct sockaddr_in servaddr;
if (argc != 2)
{
printError("Requires ip address of the server");
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printError("Unable to create a Connection");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_port = htons(13);
servaddr.sin_family = AF_INET;
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printError("Not valid IP");
}
if (connect(socketfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printError("Connection Error");
}
while ((n = read(socketfd, recvline, 4096)) > 0)
{
recvline[n] = 0;
if (fputs(recvline, stdout) == EOF)
{
printError("Fputs Error");
}
}
if (n < 0)
{
printError("Not readable");
}
return EXIT_SUCCESS;
}
Running server is always returning -1 on bind function. and running client always prints connection error.
Thank you in advance for help.
You should check with errno to find out WHY bind() is failing when it returns -1. You can use perror() to print a human-readable description of errno to the console, or at least strerror() to get that description in a string buffer that you can then do whatever you want with.
But the most likely reason for WHY bind() is failing is that you are trying to bind() your server to port 13. On most systems, ports 0-1023 are reserved for system services, so you would need to run your app with admin rights to listen on those ports.

Socket programming in C with user define ISN

I am doing a simple TCP socket programming as a test. I ported over the code from the following article:
C Socket Programming for Linux with a Server and Client Example Code
However, there are a little change I need to make, which is to set the initial sequence number (ISN) to a user-defined value on both client and server side.
Client:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
}
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
Server:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
Can someone enlighten me on which part of the code I should modify?

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

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

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