电子说
客户端的工作流程:首先调用socket函数创建一个Socket,然后调用bind函数将其与本机地址以及一个本地端口号绑定,请求连接服务器,通过新的socket向客户端发送字符串” hi,I am client!”。最后关闭该socket。
main()
{
int cli_sockfd; /*客户端SOCKET */
int addrlen;
char seraddr[14];
struct sockaddr_in ser_addr, /* 服务器的地址*/
cli_addr; /* 客户端的地址*/
char msg[MAX_MSG_SIZE]; /* 缓冲区*/
GetServerAddr( seraddr );
cli_sockfd = socket( AF_INET, SOCK_STREAM, 0 ); /*创建连接的SOCKET */
if ( ser_sockfd < 0 )
{ /*创建失败 */
fprintf( stderr, "socker Error:%sn", strerror( errno ) );
exit( 1 );
}
/* 初始化客户端地址*/
addrlen = sizeof(struct sockaddr_in);
bzero( &ser_addr, addrlen );
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl( INADDR_ANY );
cli_addr.sin_port = 0;
if ( bind( cli_sockfd, (struct sockaddr *) &cli_addr, addrlen ) < 0 )
{
/*棒定失败 */
fprintf( stderr, "Bind Error:%sn", strerror( errno ) );
exit( 1 );
}
/* 初始化服务器地址*/
addrlen = sizeof(struct sockaddr_in);
bzero( &ser_addr, addrlen );
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = inet_addr( seraddr );
ser_addr.sin_port = htons( SERVER_PORT );
if ( connect( cli_sockfd, (struct sockaddr *) &ser_addr, &addrlen ) != 0 ) / 请求连接 /
{
/*连接失败 */
fprintf( stderr, "Connect Error:%sn", strerror( errno ) );
close( cli_sockfd );
exit( 1 );
}
strcpy( msg, "hi,I am client!" );
send( sockfd, msg, sizeof(msg), 0 ); / 发送数据 /
recv( sockfd, msg, MAX_MSG_SIZE, 0 ); /* 接受数据*/
printf( "%sn", msg ); /*在屏幕上打印出来 */
close( cli_sockfd );
}
服务器的工作流程:首先调用socket函数创建一个Socket,然后调用bind函数将其与本机地址以及一个本地端口号绑定,接收到一个客户端时,服务器显示该客户端的IP地址,并将字串返回给客户端。
3、UDP/IP应用编程接口(API)
int main( int argc, char **argv )
{
int ser_sockfd;
int len;
/* int addrlen; */
socklen_t addrlen;
char seraddr[100];
struct sockaddr_in ser_addr;
/ 建立socket /
ser_sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
if ( ser_sockfd < 0 )
{
printf( "I cannot socket successn" );
return(1);
}
/ 填写sockaddr_in 结构 /
addrlen = sizeof(struct sockaddr_in);
bzero( &ser_addr, addrlen );
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = htonl( INADDR_ANY );
ser_addr.sin_port = htons( SERVER_PORT );
/*绑定客户端
- if(bind(ser_sockfd,(struct sockaddr *)&ser_addr,addrlen)<0)
- {
- printf("connect");
- return 1;
- }
- while(1)
- {
- bzero(seraddr,sizeof(seraddr));
- len=recvfrom(ser_sockfd,seraddr,sizeof(seraddr),0,(struct sockaddr*)&ser_addr,&addrlen);
- / 显示client端的网络地址 /
printf( "receive from %sn", inet_ntoa( ser_addr.sin_addr ) );
/ 显示客户端发来的字串 /
printf( "recevce:%s", seraddr );
/ 将字串返回给client端 /
sendto( ser_sockfd, seraddr, len, 0, (struct sockaddr *) &ser_addr, addrlen );
}
客户端的工作流程:首先调用socket函数创建一个Socket,填写服务器地址及端口号,从标准输入设备中取得字符串,将字符串传送给服务器端,并接收服务器端返回的字符串。最后关闭该socket。
int GetServerAddr( char * addrname )
{
printf( "please input server addr:" );
scanf( "%s", addrname );
return(1);
}
int main( int argc, char **argv )
{
int cli_sockfd;
int len;
socklen_t addrlen;
char seraddr[14];
struct sockaddr_in cli_addr;
char buffer[256];
GetServerAddr( seraddr );
/* 建立socket*/
cli_sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
if ( cli_sockfd < 0 )
{
printf( "I cannot socket successn" );
return(1);
}
/* 填写sockaddr_in*/
addrlen = sizeof(struct sockaddr_in);
bzero( &cli_addr, addrlen );
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = inet_addr( seraddr );
/* cli_addr.sin_addr.s_addr=htonl(INADDR_ANY); */
cli_addr.sin_port = htons( SERVER_PORT );
bzero( buffer, sizeof(buffer) );
/* 从标准输入设备取得字符串*/
len = read( STDIN_FILENO, buffer, sizeof(buffer) );
/* 将字符串传送给server端*/
sendto( cli_sockfd, buffer, len, 0, (struct sockaddr *) &cli_addr, addrlen );
/* 接收server端返回的字符串*/
len = recvfrom( cli_sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *) &cli_addr, &addrlen );
/* printf("receive from %sn",inet_ntoa(cli_addr.sin_addr)); */
printf( "receive: %s", buffer );
close( cli_sockfd );
}
Makefile文件为:
CC=gcc
all:server client
CFLAGS=-o
server: server.c
**(CC) **(CFLAGS) $@ server.c
client: client.c
**(CC) **(CFLAGS) $@ client.c
clean:
rm -f server client
运行结果如下图:在shell中执行make进行编译,make clean删除生成文件。
全部0条评论
快来发表一下你的评论吧 !