Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginner's Socket Programming in C
    primarykey
    data
    text
    <p>I have just started learning socket programming and am finding it pretty interesting. Currently I am making the server and the client on the same computer and hence I can have the ip address as the loopback address, 127.0.0.1 and everything seems to work fine!!</p> <p>But now I was thinking of having two computers and doing the thing.. I have the following questions - 1) Say one computer is Server and another is Client. Now, should the server code reside on the server computer and the Client code on the client one?? 2) In the server code when we are providing the ip address for bind(), it should be the ip address of the system that we can find through ipconfig or it should still remain the loopback address?? 3) In the client code, I guess the ip address of the destination should be that of the server computer right?? 4) And the last and the most important thing, HOW DO I CONNECT THE TWO COMPUTERS??</p> <p>I am attaching the simple server and client message passing code that I started out with. Kindly guide me through the changes that I need to make..</p> <p>SERVER CODE</p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #define MYPORT 3500 int main() { int sockfd; int clientfd; int bytes_read; char buf[100]; int struct_size; struct sockaddr_in my_addr; struct sockaddr_in con_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); my_addr.sin_zero[8]='\0'; bind(sockfd, (struct sockaddr*)&amp;my_addr, sizeof(struct sockaddr)); listen(sockfd,5); struct_size = sizeof(con_addr); clientfd = accept(sockfd, (struct sockaddr*)&amp;con_addr, &amp;struct_size); bytes_read = read(clientfd, buf, 100); buf[bytes_read] = '\0'; printf("Message from client:%d is %s \n",clientfd, buf); close(sockfd); close(clientfd); } </code></pre> <p>CLIENT CODE</p> <pre><code>#include&lt;sys/types.h&gt; #include&lt;sys/socket.h&gt; #include&lt;netinet/in.h&gt; #include&lt;fcntl.h&gt; #include&lt;string.h&gt; #include&lt;stdio.h&gt; #define DESTPORT 3500 int main() { struct sockaddr_in dest_addr; int sockfd = socket(AF_INET,SOCK_STREAM,0); dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(DESTPORT); dest_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); dest_addr.sin_zero[8]='\0'; connect(sockfd,(struct sockaddr*)&amp;dest_addr, sizeof(struct sockaddr)); char msg[100]; printf("Enter you message: "); gets(&amp;msg); int w = write(sockfd, msg, strlen(msg)); close(sockfd); printf("Client Dying.....\n"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload