Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
currently i am using raspberry pi 2 with jessie OS.
i want the raspberry(server) to communicate with my windows computer(client) via tcp ip.
i have added a port on my pc side.
I have a doubt regarding how to open up a port on the raspberry

the pc and raspberry are NOT connected directly. they are connected in the same network

What I have tried:

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include  < sys/types.h > 
#include < sys/socket.h >
#include < netinet/in.h >

void error(const char *msg)

int main(int argc, char *argv[])
     int sockfd, newsockfd, portno=5001;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
     if (newsockfd < 0) 
          error("ERROR on accept");
         n = read(newsockfd,buffer,255);
         if (n < 0) error("ERROR reading from socket");
         printf("Here is the message: %s\n",buffer);
         n = write(newsockfd,"I got your message",18);
        n = write(newsockfd,"Server-Message",14);
     n = write(newsockfd,"Server-Message",512);
     if (n < 0) error("ERROR writing to socket");
     return 0; 

the program executes without error. but the output i get is

ERROR, no port provided
Updated 23-Mar-16 23:52pm

Your program requires that a port number is passed on the command line. So pass that when starting it:
myserver 5001

Alternatively you can hard code the port number by changing the code as suggested by CPallini. But then you have to comment / remove the line getting the port number too:
// Ignore this when using hardcoded port number
//if (argc < 2) {
//    fprintf(stderr,"ERROR, no port provided\n");
//    exit(1);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) 
    error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
// Using hard coded port number
//portno = atoi(argv[1]);
Share this answer
violence666 24-Mar-16 7:02am    
Thank you for your solution. i did the same and it worked!

the other issue i had was with my client interacting with the server. but that was solved as well.

all i had to do was

saServer.sin_port = htons( 5001 );

instead of

saServer.sin_port =( 5001 );
Jochen Arndt 24-Mar-16 7:11am    
Thank you for your feedback and accepting the solution.
Member 13751405 29-Mar-18 5:19am    
Hi Jochen.

I have the same situation as you.
I want to connect RasperryPi(server) to windows(client).
I can run the communication between two RaspberryPis (one as server and other as a client), but not RasperryPi to windows.
I will be so grateful if you can send me your project file.
Jochen Arndt 29-Mar-18 5:27am    
I did not have a project.

I answered the question from violence666 who had a very specific problem.

If you have working Pi client code you have to convert it to be used with Windows.

While you can use raw sockets with windows, it is recommended to use WinSock which requires converting after becoming familiar with WinSock.
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");

Since you hard-wired the port number, you have to remove the above lines.
Share this answer
violence666 24-Mar-16 5:43am    
i removed the part. now i get the following displayed on my output

"Segmentation fault

program exited with code :139"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900