|
|
如下是基于BSD系统的sockets例程,在BSD系统中,可以直接修改为自己需要的代码。
server例程
[code:1:b64f5390ba]
/* vcserver.c -- TCP network (virtual circuit) server */
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* sockaddr_in structure */
/* This entry allows the program to look up the name
of the host and any alias names associated with it. */
#include <netdb.h> /* /etc/hosts table entries */
main (int argc, char *argv[])
{
int rc, /* system call return code */
new_sd, sock, /* server/listen socket descriptors */
adrlen, /* sockaddr length */
cnt; /* number of bytes I/O */
struct sockaddr_in myname; /* Internet socket name */
struct sockaddr_in *nptr; /* ptr to get port number */
struct sockaddr addr; /* generic socket name */
char buf[80]; /* I/O buffer, kind of small */
/* For lookup in /etc/hosts file. */
struct hostent *hp, *gethostbyaddr();
/* Identify the server process. */
printf("\nThis is the network server with pid %d\n",
getpid() );
/* As in UNIX domain sockets, create a "listen" socket */
if (( sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
printf("network server socket failure %d\n", errno);
perror("network server");
exit(1);
/* Initialize the fields in the Internet socket name
structure. */
myname.sin_family = AF_INET; /* Internet address */
myname.sin_port = 0; /* System will assign port # */
myname.sin_addr.s_addr = INADDR_ANY; /* "Wildcard" */
/* Bind the Internet address to the Internet socket */
if (bind(sock, &myname, sizeof(myname) ) < 0 ) {
close(sock); /* defensive programming */
printf("network server bind failure %d\n", errno);
perror("network server");
exit(2);
}
/* Get the port number assigned to the Internet socket.
getsockname() obtains the port number associated
with the bound socket and returns it as part of the
information in the sockaddr addr structure. Note
that, since the port number is not passed directly
by this program to any client, the only way to
"advertise" it is to print it, that is, send it to
the user's stdout. Other than this printout, this
code is not intrinsic to the connectivity process.
*/
adrlen = sizeof(addr); /* need int for return value */
if ( ( rc = getsockname( sock, &addr, &adrlen ) ) < 0 )
{
printf("setwork server getsockname failure %d\n",
errno);
perror("network server");
close (sock);
exit(3);
}
/* DEBUG CODE: the generic address "addr" is used to
return the socket value obtained from the
getsockname() call. Print this information. In the
generic structure definition, all but the address
family is defined as a char string. After this
call, the generic address structure addr is used to
hold information about the client process. */
printf("\nAfter getsockname():");
printf(" server listen socket data\n");
printf("\taddr.sa_family field value is: %d\n",
addr.sa_family);
printf("\taddr.sa_data string is %d bytes long;\n",
sizeof ( addr.sa_data ) );
printf("\taddr.sa_data string is:");
for ( cnt = 0: cnt < sizeof (addr.sa_data); cnt++)
printf(" %x", addr.sa_data[cnt]);
printf("\n");
/* Now "advertise" the port number assigned to the
socket. In this example, this port number must be
used as the second command line parameter when
starting up the client process. */
/* Note the use of the pointer nptr, with a different
mapping of the allocated memory, to point to the
generic address structure. */
nptr = (struct sockaddr_in *) & /* port # */
printf("\n\tnetwork server: server has port number: %d\n",
ntohs ( nptr -> sin_port ) );
/* Mark the socket as a "listen-only" or passive socket */
if ( listen ( sock, 5 ) < 0 ) {
printf("network server bind failure %d\n", errno);
perror("network server");
close (sock);
exit(4);
}
/* Debug output: information contained in myname structure
(the Internet socket). */
printf("Server has set up client socket with values:\n");
printf("\tInternet address is %lx\n", myname.sin_addr.s_addr);
printf("\tPort number used is %d\n", myname.sin_port);
printf("\tInternet family ID is %d\n", myname.sin_family);
printf("\tValues are filled in after connection request ");
printf("is accepted.");
/* Set up "infinite loop" to listen for clients. Since the
structure "myname" is bound to the listen socket, the
socket structure name and socket length parameter
values are omitted from the accept call. The bound values
are used. */
while (1) {
if ( ( new_sd = accept ( sock, 0, 0 ) ) < 0 ) {
printf("network server accept failure %d\n", errno);
perror("network server");
close (sock);
exit(5);
}
/* Fork child process to handle client service request */
if ( ( fork() ) == 0 ) { /* Child process */
int pid;
pid = getpid(); /* PID of child process */
close (sock); /* Do not need listen socket in child. */
/* Find out who the client is. Note the use of the
generic address structure addr to hold information
about the (connected) client. */
if ((rc = getpeername( new_sd, &addr, &adrlen )) < 0) {
printf("network server %d getpeername failure %d\n",
pid, errno);
perror("network server");
close(new_sd);
exit(6);
}
/* Just for grins, "announce" the client. Note that,
since pointer nptr is of type struct sockaddr_in,
the field names as defined in the structure template
sockaddr_in can be used to access values in the addr
generic structure. */
printf("\n\tnetwork server %d:", pid);
printf(" client socket from host %s\n",
inet_ntoa ( nptr -> sin_addr ) );
printf("\t has port number %d\n",nptr -> sin_port);
/* Now find all names associated with the client; this
is the reason for the /etc/hosts file lookup
declarations. */
if (( hp = gethostbyaddr (&nptr -> sin_addr,4,AF_INET))
!= NULL ) {
printf ("\tfrom hostname: %s\n\twith aliases: ",
hp -> h_name );
while ( *hp -> h_aliases )
printf ("\n\t\t\t%s", *hp -> h_aliases++ );
printf("\n\n");
}
else {
printf("network server %d ", pid);
printf("gethostbyaddr failure %d\n", h_errno);
perror("network server");
}
/* Exchange data with client. Clear buffer first. */
do {
/* Take your pick, depending on system pedigree.
The System V function has not been tested as of
this edition. */
bzero( buf, sizeof(buf)); /* zero buf, BSD call. */
/* memset (buf,0,sizeof(buf)); /* zero buf, S5. */
/* Read message from remote client; if message length
= 0, quit. */
if (( cnt = read (new_sd, buf, sizeof(buf))) < 0 ) {
printf("network server %d ", pid);
printf("socket read failure &d\n", errno);
perror("network server");
close(new_sd);
exit(7);
}
else
if (cnt == 0) {
printf("network server received message");
printf(" of length %d\n", cnt);
printf("network server closing");
printf(" client connection...\n");
close (new_sd);
continue; /* break out of loop */
}
else {
/* Print out message received from client. Send
a message back. */
printf("network server %d received message",pid);
printf(" of length %d\n", cnt);
printf("network server %d received", pid));
printf(" the message %s\n", buf);
bzero (buf, sizeof(buf)); /* zero buf, BSD. */
/* memset(buf,0,sizeof(buf)); /* zero buf, S5. */
strcpy(buf, "Message from server to client");
write (new_sd, buf, sizeof(buf));
} /* end of message-print else */
} /* end of do loop statement */
while (cnt != 0); /* do loop condition */
exit(0); /* Exit child process */
} /* End of if-child-process true condition */
else /* Not child process; must be parent process */
close (new_sd); /* Parent doesn't need work socket. */
} /* end of while (1) */
} /* end of main procedure */
[/code:1:b64f5390ba]
client例程
[code:1:b64f5390ba]
/* vcclient.c -- TCP network (virtual circuit) client */
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* sockaddr_in structure */
/* This entry allows the program to look up the name
of the host and any alias names associated with it. */
#include <netdb.h> /* /etc/hosts table entries */
main (argc, argv)
int argc;
char *argv[];
/* Expected command line parameters:
argv[0] -- name of executable
argv[1] -- the host name to which connection is desired
argv[2] -- the port number to be used by the client: the
value is the port number assigned to the
server by the server's host system.
This is not elegant, but is very useful for debugging the
connectivity code. For example, if the host name ( argv[1] )
were speficied as "localhost" both client and server
could run on the same system but the network connectivity
code would still be fully exercised. */
{
int sock, /* socket descriptor */
val, /* scratch variable */
cnt; /* number of bytes I/O */
struct sockaddr_in myname; /* Internet socket name (addr) */
struct sockaddr_in *nptr; /* pointer to get port number */
char buf[80]; /* I/O buffer, kind of small */
/* For lookup in /etc/hosts file. */
struct hostent *hp, *gethostbyaddr();
/* Check that the user supplied all parameters on the
command line. If so, convert argv[2] to integer; copy it
into the sin_port field of the myname structure. Use
the htons function to insure that the value is stored
in network byte order. */
if ( argc < 3 ) {
printf("network client failure: required parameters");
printf(" missing from the command line\n");
printf("network client: usage");
printf("[executable-name] [host name] [port number]\n");
exit(1);
}
/* As in UNIX domain, create a client socket to request
service */
if (( sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
printf("network client socket failure %d\n", errno);
perror("network client");
exit(2);
}
/* Convert user-supplied port number to integer. There is
no consistency check possible in this set-up. */
myname.sin_port = htons( atoi(argv[2]) ); /* Server port # */
myname.sin_family = AF_INET; /* Internet domain */
/* For display purposes only, print out the host name and
the converted port number. */
printf("network client %s will try to connect to host %s\n",
argv[0], argv[1]);
printf("network client %s will use port number %d\n",
argv[0], ntohs ( myname.sin_port ) );
/* Obtain server host information. */
hp = gethostbyname ( argv[1] );
/* This is mainly debug code; if it becomes necessary to
insert it (or if it just feels more comfortable to
have it in place!!!) don't remove it when all is working
well. Rather, leave it in place as a comment. */
if ( hp == NULL ) {
printf("network client gethostbyname failure %d\n",
errno);
perror("network client");
close ( sock );
exit(3);
}
else {
printf("\nServer information obtained via");
printf(" gethostbyname:\n");
printf("\tThe official host name is %s\n", hp -> h_name);
printf("\tThe address type is %d\n", hp -> h_addrtype);
printf("\tThe length of the address is %d bytes\n",
hp -> h_length);
printf("\tThe first host address is %lx\n",
ntohl ( * (int * ) hp -> h_addr_list[0] ) );
printf("\tAlias names for the host are:\n");
while ( *hp -> h_aliases )
printf( "\t\t%s\n", *hp -> h_aliases++ );
}
/* Use either memcpy or bcopy as appropriate
(System V vs BSD). */
bcopy ( hp -> h_addr_list[0], &myname.sin_addr.s_addr,
hp -> h_length );
/* memcpy ( &myname.sin_addr.s_addr, hp -> h_addr_list[0],
hp -> h_length ); */
/* More debug code: Verify the contents of structure myname
prior to trying to connect to the (remote) server. */
printf("\nInformation provided in client's");
printf(" connect request\n");
printf("\tRemote host address is %lx\n",
ntohl ( myname.sin_addr.s_addr ) );
printf("\tPort number supplied is %d\n",
ntohs ( myname.sin_port ) );
printf("\tInternet family ID is %d\n", myname.sin_family);
printf("\tsin_zero character string is: %s\n",
myname.sin_zero);
/* Establish socket connection with (remote) server. */
if ( ( connect ( sock, &myname, sizeof(myname) ) ) < 0 ) {
printf("network client %s connect failure %d\n",
argv[0], errno);
perror("network client");
close (sock);
exit(4);
}
/* Exchange data with client. Clear buffer bytes first. */
/* Take your pick, depending on your system's pedigree. The
System V function has not been tested. */
bzero ( buf, sizeof( buf) ); /* zero buffer, BSD. */
/* memset ( buf, 0, sizeof( buf) ); /* zero buffer S5. */
strcpy ( buf, "Message from client to server" );
write ( sock, buf, sizeof(buf) );
/* Now read message sent back by server. */
if ( ( cnt = read (sock, buf, sizeof(buf) ) ) < 0 ) {
printf("network client socket read failure &d\n", errno);
perror("network client");
close(sock);
exit(5);
}
else
printf("network client received the message %s\n", buf);
/* Now send a message with 0 bytes. */
bzero ( buf, sizeof( buf) ); /* zero buffer, BSD. */
/* memset ( buf, 0, sizeof( buf) ); /* zero buffer S5. */
write ( sock, buf, 0 );
close (sock);
exit(0);
} /* end of main procedure */
[/code:1:b64f5390ba]
| 蓝色键盘 回复于:2003-05-09 17:02:44
| 有时间,继续补充!
也请各位把你们认为不错的回复在这个后面,以后大家查阅方便一些!
| | stevenyi 回复于:2003-05-10 00:20:30
| 这个论坛不便于收藏帖子,如果可以选择“我参与的帖子”就好了
| | shangxd 回复于:2003-05-10 12:59:18
| 下面是修改过的,只是一些小的修改,编译环境ALPHA FREEBSD4.7
gcc version 2.95.4 20020320 [FreeBSD]
SERVER.C
[code:1:ea111457c2]
/* vcserver.c -- TCP network (virtual circuit) server */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* sockaddr_in structure */
#include <arpa/inet.h>
#include <string.h>
/*
* This entry allows the program to look up the name of the host and any
* alias names associated with it.
*/
#include <netdb.h> /* /etc/hosts table entries */
int
main(void)
{
int rc, /* system call return code */
new_sd, sock, /* server/listen socket descriptors */
adrlen, /* sockaddr length */
cnt; /* number of bytes I/O */
struct sockaddr_in myname; /* Internet socket name */
struct sockaddr_in *nptr; /* ptr to get port number */
struct sockaddr addr; /* generic socket name */
char buf[80];/* I/O buffer, kind of small */
/* For lookup in /etc/hosts file. */
struct hostent *hp, *gethostbyaddr();
/* Identify the server process. */
printf("\nThis is the network server with pid %d\n", getpid());
/* As in UNIX domain sockets, create a "listen" socket */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("network server socket failure %d\n", errno);
perror("network server");
exit(1);
}
/*
* Initialize the fields in the Internet socket name structure.
*/
myname.sin_family = AF_INET; /* Internet address */
myname.sin_port = 0; /* System will assign port # */
myname.sin_addr.s_addr = INADDR_ANY; /* "Wildcard" */
/* Bind the Internet address to the Internet socket */
if (bind(sock, (struct sockaddr *) & myname, sizeof(myname)) < 0) {
close(sock); /* defensive programming */
printf("network server bind failure %d\n", errno);
perror("network server");
exit(2);
}
/*
* Get the port number assigned to the Internet socket. getsockname()
* obtains the port number associated with the bound socket and
* returns it as part of the information in the sockaddr addr
* structure. Note that, since the port number is not passed
* directly by this program to any client, the only way to
* "advertise" it is to print it, that is, send it to the user's
* stdout. Other than this printout, this code is not intrinsic to
* the connectivity process.
*/
adrlen = sizeof(addr); /* need int for return value */
if ((rc = getsockname(sock, &addr, &adrlen)) < 0) {
printf("setwork server getsockname failure %d\n",
errno);
perror("network server");
close(sock);
exit(3);
}
/*
* DEBUG CODE: the generic address "addr" is used to return the
* socket value obtained from the getsockname() call. Print this
* information. In the generic structure definition, all but the
* address family is defined as a char string. After this call, the
* generic address structure addr is used to hold information about
* the client process.
*/
printf("\nAfter getsockname():");
printf(" server listen socket data\n");
printf("\taddr.sa_family field value is: %d\n", addr.sa_family);
printf("\taddr.sa_data string is %ld bytes long;\n", sizeof(addr.sa_data));
printf("\taddr.sa_data string is:");
for (cnt = 0; cnt < sizeof(addr.sa_data); cnt++)
printf(" %x", addr.sa_data[cnt]);
printf("\n");
/*
* Now "advertise" the port number assigned to the socket. In this
* example, this port number must be used as the second command line
* parameter when starting up the client process.
*/
/*
* Note the use of the pointer nptr, with a different mapping of the
* allocated memory, to point to the generic address structure.
*/
nptr = (struct sockaddr_in *) & addr; /* port # */
printf("\n\tnetwork server: server has port number: %d\n", ntohs(nptr->sin_port));
/* Mark the socket as a "listen-only" or passive socket */
if (listen(sock, 5) < 0) {
printf("network server bind failure %d\n", errno);
perror("network server");
close(sock);
exit(4);
}
/*
* Debug output: information contained in myname structure (the
* Internet socket).
*/
printf("Server has set up client socket with values:\n");
printf("\tInternet address is %u\n", ntohl(myname.sin_addr.s_addr));
printf("\tPort number used is %d\n", myname.sin_port);
printf("\tInternet family ID is %d\n", myname.sin_family);
printf("\tValues are filled in after connection request ");
printf("is accepted.");
/*
* Set up "infinite loop" to listen for clients. Since the structure
* "myname" is bound to the listen socket, the socket structure name
* and socket length parameter values are omitted from the accept
* call. The bound values are used.
*/
while (1) {
if ((new_sd = accept(sock, 0, 0)) < 0) {
printf("network server accept failure %d\n", errno);
perror("network server");
close(sock);
exit(5);
}
/* Fork child process to handle client service request */
if ((fork()) == 0) { /* Child process */
int pid;
pid = getpid(); /* PID of child process */
close(sock); /* Do not need listen socket in
* child. */
/*
* Find out who the client is. Note the use of the
* generic address structure addr to hold information
* about the (connected) client.
*/
if ((rc = getpeername(new_sd, &addr, &adrlen)) < 0) {
printf("network server %d getpeername failure %d\n",
pid, errno);
perror("network server");
close(new_sd);
exit(6);
}
/*
* Just for grins, "announce" the client. Note that,
* since pointer nptr is of type struct sockaddr_in,
* the field names as defined in the structure
* template sockaddr_in can be used to access values
* in the addr generic structure.
*/
printf("\n\tnetwork server %d:", pid);
printf(" client socket from host %s\n",
inet_ntoa(nptr->sin_addr));
printf("\t has port number %d\n", nptr->sin_port);
/*
* Now find all names associated with the client;
* this is the reason for the /etc/hosts file lookup
* declarations.
*/
if ((hp = gethostbyaddr(&nptr->sin_addr, 4, AF_INET))
!= NULL) {
printf("\tfrom hostname: %s\n\twith aliases: ",
hp->h_name);
while (*hp->h_aliases)
printf("\n\t\t\t%s", *hp->h_aliases++);
printf("\n\n");
} else {
printf("network server %d ", pid);
printf("gethostbyaddr failure %d\n", h_errno);
perror("network server");
}
/* Exchange data with client. Clear buffer first. */
do {
/*
* Take your pick, depending on system
* pedigree. The System V function has not
* been tested as of this edition.
*/
bzero(buf, sizeof(buf)); /* zero buf, BSD call. */
memset(buf, 0, sizeof(buf)); /* zero buf, S5. */
/*
* Read message from remote client; if
* message length = 0, quit.
*/
if ((cnt = read(new_sd, buf, sizeof(buf))) < 0) {
printf("network server %d ", pid);
printf("socket read failure &d\n", errno);
perror("network server");
close(new_sd);
exit(7);
} else if (cnt == 0) {
printf("network server received message");
printf(" of length %d\n", cnt);
printf("network server closing");
printf(" client connection...\n");
close(new_sd);
continue; /* break out of loop */
} else {
/*
* Print out message received from
* client. Send a message back.
*/
printf("network server %d received message", pid);
printf(" of length %d\n", cnt);
printf("network server %d received", pid);
printf(" the message %s\n", buf);
bzero(buf, sizeof(buf)); /* zero buf, BSD. */
memset(buf, 0, sizeof(buf)); /* zero buf, S5. */
strcpy(buf, "Message from server to client");
write(new_sd, buf, sizeof(buf));
} /* end of message-print else */
} /* end of do loop statement */
while (cnt != 0); /* do loop condition */
exit(0);/* Exit child process */
}
/* End of if-child-process true condition */
else /* Not child process; must be parent process */
close(new_sd); /* Parent doesn't need work socket. */
} /* end of while (1) */
return 0;
} /* end of main procedure */
[/code:1:ea111457c2]
CLIENT.C
[code:1:ea111457c2]
/* vcclient.c -- TCP network (virtual circuit) client */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* sockaddr_in structure */
/*
* This entry allows the program to look up the name of the host and any
* alias names associated with it.
*/
#include <netdb.h> /* /etc/hosts table entries */
#include <stdlib.h>
int
main(int argc, char *argv[])
/*
* Expected command line parameters:
*
* argv[0] -- name of executable argv[1] -- the host name to which connection is
* desired argv[2] -- the port number to be used by the client: the value is
* the port number assigned to the server by the server's host system.
*
* This is not elegant, but is very useful for debugging the connectivity code.
* For example, if the host name ( argv[1] ) were speficied as "localhost"
* both client and server could run on the same system but the network
* connectivity code would still be fully exercised.
*/
{
int sock, /* socket descriptor */
cnt; /* number of bytes I/O */
struct sockaddr_in myname; /* Internet socket name (addr) */
char buf[80];/* I/O buffer, kind of small */
/* For lookup in /etc/hosts file. */
struct hostent *hp, *gethostbyaddr();
/*
* Check that the user supplied all parameters on the command line.
* If so, convert argv[2] to integer; copy it into the sin_port field
* of the myname structure. Use the htons function to insure that
* the value is stored in network byte order.
*/
if (argc < 3) {
printf("network client failure: required parameters");
printf(" missing from the command line\n");
printf("network client: usage");
printf("[executable-name] [host name] [port number]\n");
exit(1);
}
/*
* As in UNIX domain, create a client socket to request service
*/
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("network client socket failure %d\n", errno);
perror("network client");
exit(2);
}
/*
* Convert user-supplied port number to integer. There is no
* consistency check possible in this set-up.
*/
myname.sin_port = htons(atoi(argv[2])); /* Server port # */
myname.sin_family = AF_INET; /* Internet domain */
/*
* For display purposes only, print out the host name and the
* converted port number.
*/
printf("network client %s will try to connect to host %s\n",
argv[0], argv[1]);
printf("network client %s will use port number %d\n",
argv[0], ntohs(myname.sin_port));
/* Obtain server host information. */
hp = gethostbyname(argv[1]);
/*
* This is mainly debug code; if it becomes necessary to insert it
* (or if it just feels more comfortable to have it in place!!!)
* don't remove it when all is working well. Rather, leave it in
* place as a comment.
*/
if (hp == NULL) {
printf("network client gethostbyname failure %d\n",
errno);
perror("network client");
close(sock);
exit(3);
} else {
printf("\nServer information obtained via");
printf(" gethostbyname:\n");
printf("\tThe official host name is %s\n", hp->h_name);
printf("\tThe address type is %d\n", hp->h_addrtype);
printf("\tThe length of the address is %d bytes\n",
hp->h_length);
printf("\tThe first host address is %u\n", ntohl(*(int *)hp->h_addr_list[0]));
printf("\tAlias names for the host are:\n");
while (*hp->h_aliases)
printf("\t\t%s\n", *hp->h_aliases++);
}
/*
* Use either memcpy or bcopy as appropriate (System V vs BSD).
*/
bcopy(hp->h_addr_list[0], &myname.sin_addr.s_addr,
hp->h_length);
/*
* memcpy ( &myname.sin_addr.s_addr, hp -> h_addr_list[0], hp ->
* h_length );
*/
/*
* More debug code: Verify the contents of structure myname prior to
* trying to connect to the (remote) server.
*/
printf("\nInformation provided in client's");
printf(" connect request\n");
printf("\tRemote host address is %u\n", ntohl(myname.sin_addr.s_addr));
printf("\tPort number supplied is %d\n",
ntohs(myname.sin_port));
printf("\tInternet family ID is %d\n", myname.sin_family);
printf("\tsin_zero character string is: %s\n",
myname.sin_zero);
/* Establish socket connection with (remote) server. */
if ((connect(sock, (struct sockaddr *) & myname, sizeof(myname))) < 0) {
printf("network client %s connect failure %d\n",
argv[0], errno);
perror("network client");
close(sock);
exit(4);
}
/* Exchange data with client. Clear buffer bytes first. */
/*
* Take your pick, depending on your system's pedigree. The System V
* function has not been tested.
*/
bzero(buf, sizeof(buf));/* zero buffer, BSD. */
memset(buf, 0, sizeof(buf)); /* zero buffer S5. */
strcpy(buf, "Message from client to server");
write(sock, buf, sizeof(buf));
/* Now read message sent back by server. */
if ((cnt = read(sock, buf, sizeof(buf))) < 0) {
printf("network client socket read failure &d\n", errno);
perror("network client");
close(sock);
exit(5);
} else
printf("network client received the message %s\n", buf);
/* Now send a message with 0 bytes. */
bzero(buf, sizeof(buf));/* zero buffer, BSD. */
memset(buf, 0, sizeof(buf)); /* zero buffer S5. */
write(sock, buf, 0);
close(sock);
exit(0);
} /* end of main procedure */
[/code:1:ea111457c2]
编译的时候应该没有什么警告了!谢谢楼主提供源码!
| |
|