The feasibility of using char * STR for the second parameter of send (;) in unixdomaisocket

Source: Internet
Author: User

The content to be written today is to test whether the second parameter in send (,) can use the char * STR pointer.

Conclusion: Yes!

The server code is as follows:

/* 2012-06-07-the code is modified based on the http://beej.us/guide/bgipc/output/html/multipage/unixsock.html. Download the original code * echos. C -- the echo server for echoc. c; demonstrates UNIX sockets */# include <stdio. h> # include <stdlib. h> # include <errno. h> # include <string. h> # include <sys/types. h> # include <sys/socket. h> # include <sys/UN. h> # define sock_path "/home/Tian/mysocket" int main (void) {int S, S2, T, Len; struct sockaddr_un local, remote; char STR [100]; if (S = socket (af_unix, sock_stream, 0) =-1) {perror ("socket"); exit (1) ;} local. sun_family = af_unix; strcpy (local. sun_path, sock_path); unlink (local. sun_path); Len = strlen (local. sun_path) + sizeof (local. sun_family); If (BIND (S, (struct sockaddr *) & Local, Len) =-1) {perror ("bind"); exit (1 );} if (Listen (S, 5) =-1) {perror ("listen"); exit (1) ;}for (;) {int done, N; printf ("waiting for a connection... \ n "); t = sizeof (remote); If (s2 = accept (S, (struct sockaddr *) & remote, & T) =-1) {perror ("accept"); exit (1);} printf ("connected. \ n "); do {memset (STR, 0, sizeof (STR); n = Recv (S2, STR, 5, 0); If (n <0) {perror ("Recv"); break;} else if (n = 0) {break;} // printf ("echo> % s \ n", STR ); //} while (1); close (S2);} return 0 ;}

Server execution result:

The client code is as follows:

/* 2012-06-07-the code is modified based on the http://beej.us/guide/bgipc/output/html/multipage/unixsock.html. Download the original code ** echoc. C -- the echo client for echos. c; demonstrates UNIX sockets */# include <stdio. h> # include <stdlib. h> # include <errno. h> # include <string. h> # include <sys/types. h> # include <sys/socket. h> # include <sys/UN. h> # define sock_path "/home/Tian/mysocket" int main (void) {int S, T, Len; struct sockaddr_un remote; If (S = socket (af_unix, sock_stream, 0) =-1) {perror ("socket"); exit (1);} printf ("t Rying to connect... \ n "); remote. sun_family = af_unix; strcpy (remote. sun_path, sock_path); Len = strlen (remote. sun_path) + sizeof (remote. sun_family); If (connect (S, (struct sockaddr *) & remote, Len) =-1) {perror ("Connect"); exit (1 );} printf ("connected. \ n "); int num = 5; char * STR = (char *) calloc (5, sizeof (char); While (1) {sprintf (STR, "% d", num); // format num in 10 to STR // printf ("strlen --- % d \ n ", Strlen (STR); // print the STR length // If (send (S, STR, strlen (STR), 0) =-1) {perror ("send"); exit (1);} printf ("Num --- % d \ n", num); // print num to judge while () whether the loop runs num --; If (num = 0) break;} If (STR! = NULL) {STR = NULL; free (STR); // release the memory space occupied by STR} Close (s); Return 0 ;}

Client execution result:

Based on the execution results of the server and client, you can determine that the program runs normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.