Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just created 2 programs to show minimum requirements to communicate a client/server with sockets. You can google for each function to learn their prototypes and how they work.</p> <p>Here it is:</p> <h2>Minimum Server (minser.c)</h2> <pre><code>/* Program: minser.c Author: Dr Beco, 2011-04-03 Objective: show a minimum server program that can create a socket, accept a client, read a byte, write a byte, disconnect */ #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;netinet/in.h&gt; #include &lt;sys/un.h&gt; #define BUFFER 2 int main(void) { printf("Configuring environment... "); int p = 3333; // port char data[BUFFER]; struct sockaddr_in dir; struct sockaddr client; socklen_t long_client; int id, idReuse=1, son, aux; memset(&amp;dir,0,sizeof(dir)); dir.sin_port = p; dir.sin_family = AF_INET; dir.sin_addr.s_addr = INADDR_ANY; printf("done!\n"); printf("Creating socket... "); id = socket(AF_INET, SOCK_STREAM, 0); if (id == -1) return -1; printf("done!\n"); printf("Configuring socket... "); if(setsockopt(id,SOL_SOCKET,SO_REUSEADDR,&amp;idReuse,sizeof(idReuse))==-1) return -1; printf("done!\n"); printf("Binding... "); if(bind(id, (struct sockaddr *)&amp;dir, sizeof(dir)) == -1) { close (id); return -1; } printf("done!\n"); printf("Listening... "); if (listen(id , 1) == -1) { close(id); return -1; } printf("done!\n"); printf("Accepting... "); long_client = sizeof (client); son = accept(id, &amp;client, &amp;long_client); if (son == -1) return -1; printf("done!\n"); printf("Reading... "); aux = read(son, data , 1); if(aux!=1) return -1; printf("\"%c\" ", data[0]); printf("done!\n"); printf("Writing \"S\"... "); aux = send(son, "S", 1, MSG_NOSIGNAL); if(aux &lt; 0) return -1; printf("done!\n"); return 0; } </code></pre> <h2>Minimun Client (mincli.c)</h2> <pre><code>/* Program: mincli.c Author: Dr Beco, 2011-04-03 Objective: show a minimum client program that can connect to a network, write a byte, read a byte, disconnect */ #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;netinet/in.h&gt; #include &lt;netdb.h&gt; #define BUFFER 2 int main(void) { printf("Configuring environment... "); char data[BUFFER]; const char *host_server="localhost"; struct sockaddr_in dir; struct hostent *host; int aux, id, p=3333; //port dir.sin_port = p; dir.sin_family = AF_INET; host = gethostbyname(host_server); if(host == NULL) return -1; dir.sin_addr.s_addr = ((struct in_addr *)(host-&gt;h_addr))-&gt;s_addr; printf("done!\n"); printf("Creating socket... "); id = socket(AF_INET, SOCK_STREAM, 0); if(id == -1) return -1; printf("done!\n"); printf("Connecting... "); if(connect(id, (struct sockaddr *)&amp;dir, sizeof(dir)) == -1) return -1; printf("done!\n"); printf("Writing \"C\"... "); aux = send(id, "C", 1, MSG_NOSIGNAL); if(aux &lt; 0) return -1; printf("done!\n"); printf("Reading... "); aux = read(id, data , 1); if(aux!=1) return -1; printf("\"%c\"", data[0]); printf(" done!\n"); return 0; } </code></pre> <p>Take care, Beco</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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