第一个例子很简单,单服务器单客户端。出自csapp,稍微做了一丁点改变。
建立链接后,客户端向服务器发送字符串,并把字符串显示在终端;服务器接受到字符串后,显示长度,并将字符串显示在终端。客户端如果遇到ctrl+c或ctrl+d意外中止,可以重新连接服务器。
客户端代码:
- /* echoClient.c
-
* Author: toyxia
-
*/
-
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netdb.h>
-
#include <netinet/in.h>
-
#include <strings.h>
-
#include <unistd.h>
-
#include <errno.h>
-
#include <ctype.h>
-
-
int writen( int fd, char * usrbuf, int n )
-
{
-
char * buf = usrbuf;
-
int nleft = n;
-
int nwritten;
-
while( nleft > 0 )
-
{
-
if( (nwritten = write(fd, buf, n)) <= 0 )
-
{
-
if( errno == EINTR )
-
{
-
nwritten = 0;
-
}
-
else return -1;
-
}
-
nleft-=nwritten;
-
buf+=nwritten;
-
}
-
return n;
-
}
-
-
-
int main( int argc, char ** argv )
-
{
-
-
/*Defenition*/
-
int clientfd;
-
struct hostent * hep;
-
struct sockaddr_in serverAddr;
-
int echoPort=3333;
-
char buf[100];
-
int readChar=0;
-
-
if( argc != 2 )
-
{
-
printf( "Usage: %s hostname\n ",argv[0]);
-
return -1;
-
}
-
-
/*create socket */
-
if( (clientfd = socket(AF_INET, SOCK_STREAM, 0 )) < 0 )
-
{
-
perror( "socket error\n" );
-
return -1;
-
}
-
-
/* create host entry */
-
if( ( hep = gethostbyname(argv[1]) ) == NULL )
-
{
-
perror( "gethostname error\n" );
-
return -1;
-
}
-
-
/* set clientAddr */
-
bzero( ( char* )&serverAddr, sizeof(serverAddr) );
-
serverAddr.sin_family = AF_INET;
-
serverAddr.sin_port = htons(echoPort);
-
serverAddr.sin_addr = *( (struct in_addr *)hep->h_addr );
-
-
/* connect to server */
-
if( ( connect( clientfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr) ) ) < 0 )
-
{
-
perror("connect error\n");
-
return -1;
-
}
-
-
/* read from stdio, and write to server */
-
while( (readChar = read(STDIN_FILENO, buf, 33)) != 0 )
-
{
-
printf("read %d char from stdio ", readChar);
-
if( writen( clientfd, buf,readChar ) == -1 )
-
{
-
perror("write error\n");
-
}
-
-
buf[ readChar ] = '\0';
-
printf( "client echo: %s", buf );
-
}
-
-
close( clientfd );
-
return 0;
-
-
}
服务器端:
- /* echoServer.c
-
* Author: toyxia
-
*/
-
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <sys/socket.h>
-
#include <netdb.h>
-
#include <netinet/in.h>
-
#include <sys/types.h>
-
#include <strings.h>
-
#include <arpa/inet.h>
-
-
#define BACKLOG 5
-
-
int main( int argc, char ** argv )
-
{
-
/* Definition */
-
-
int serverfd, confd;
-
struct hostent * hep;
-
struct sockaddr_in serverAddr, clientAddr;
-
int echoPort = 3333;
-
int readChar=0;
-
char buf[100];
-
int addLen;
-
-
/*create socket*/
-
if( (serverfd = socket(AF_INET, SOCK_STREAM,0)) < 0 )
-
{
-
perror("create socket error\n");
-
return -1;
-
}
-
-
/* create host entry */
-
bzero( (char *)&serverAddr, sizeof(serverAddr) );
-
serverAddr.sin_family = AF_INET;
-
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
serverAddr.sin_port = htons(echoPort);
-
-
/*binding the socket*/
-
if( ( bind( serverfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr) ) ) < 0 )
-
{
-
perror( "binding socket error\n" );
-
return -1;
-
}
-
-
/* listen the socket */
-
if( listen( serverfd,BACKLOG ) == -1 )
-
{
-
perror( "lister error\n" );
-
return -1;
-
}
-
-
/* accept the request */
-
while(1)
-
{
-
/* get the connectino ip */
-
addLen= sizeof(serverAddr);
-
confd = accept( serverfd, (struct sockaddr *)&clientAddr, &addLen);
-
printf( "got connection from %s\n",inet_ntoa(clientAddr.sin_addr) );
-
-
/* read the message from client */
-
while( 1 )
-
{
-
if( ( readChar = read(confd,buf,100) ) == -1 )
-
{
-
-
perror("read error\n");
-
return -1;
-
}
-
else if( readChar == 0 )
-
{
-
perror( "EOF, detach\n" );
-
break;
-
}
-
-
printf("read %d char from client\n", readChar-1);
-
buf[readChar] = '\0';
-
printf("server echo: %s", buf);
-
-
}
-
-
close(confd);
-
}
-
-
close(serverfd);
-
}
阅读(1119) | 评论(0) | 转发(0) |