注意:可以开启一个server和多个client,同时可以对server进行发送data处理,请注意client的参数,格式是:./client IP char_data
IP:就是server所在的IP,char_data就是发送的数据
server.c
#include<sys/types.h>
#include<sys/socket.h>
#include<strings.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<sys/wait.h>
#define
LISTEN_PORT 6000 //!> 端口
#define MAX 5 //!> 最大的等待请求数目
void str_echo( int sockfd) //!> 从client中读取数据
{
ssize_t n;
charline[512];
printf("准备读数据:");
while( 1)
{
while( ( n =read( sockfd, line, 512 ) ) > 0) //!> 注意是:根据accept得到的client的操作描述符来处理的呗~
{
line[n] ='\0';
printf("Client send: %s\n", line );
bzero(&line, sizeof( line )); //!> 再次置NULL
}
}
}
int main( int argc, char ** argv )
{
int listenfd,connfd; //!> 描述符
pid_t childpid; //!> 子进程号
socklen_t chilen; //!>
structsockaddr_in chiaddr, servaddr;
//!> 建立socket( TCP )
//!>
if( (listenfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
printf("socket error... : %s\n",(char*)strerror(errno));
exit(EXIT_FAILURE );
}
//!> socket参数
//!>
servaddr.sin_family =AF_INET; //!> 协议族
servaddr.sin_port = htons( LISTEN_PORT); //!> IP
servaddr.sin_addr.s_addr =INADDR_ANY; //!> 本机IP
bzero(&( servaddr.sin_zero ), 8); //!> 8字节填充字符
//!> 绑定IP和端口
//!>
if( bind(listenfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 )
{
printf("bind失败\n");
exit(EXIT_FAILURE );
}
//!> 下面开始监听
//!>
if( listen(listenfd, MAX ) == -1 )
{
printf("listen error...");
exit(EXIT_FAILURE );
}
while( 1)
{
chilen =sizeof( chiaddr );
//!> 等待连接
//!>
if( ( connfd= accept( listenfd, ( struct sockaddr* )&chiaddr,&chilen ) ) == -1 )
{
printf("accept error...\n");
exit(EXIT_FAILURE );
}
else
{
printf("client连接成功!\n");
}
//!> 创建子进程来处理请求
//!>
if( (childpid = fork() ) == 0 )
{
close(listenfd ); //!> server关闭与其链接,交给子进程处理
printf("Client from %s\n", inet_ntoa( chiaddr.sin_addr ));
str_echo(connfd );
exit(EXIT_SUCCESS );
}
else if(childpid < 0 )
{
printf("forkerror...\n");
close(connfd );
exit(EXIT_FAILURE );
}
close(connfd );
}
return0;
}
client.c
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<strings.h>
#include<string.h>
#include<arpa/inet.h>
#include<errno.h>
#define SERVER_PORT 6000
void str_cli( char * data, int sockfd) //!> 此处是client发送消息而已
{
while( 1)
{
write(sockfd, data, strlen( data ) );
sleep( 1);
}
exit( 0);
}
int main( int argc, char ** argv )
{
intsockfd;
structsockaddr_in servaddr;
if( argc !=3 )
{
printf("输入参数!\n");
exit(EXIT_FAILURE );
}
if( ( sockfd= socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
printf("socket error...\n");
exit(EXIT_FAILURE );
}
bzero(&servaddr, sizeof( servaddr ) );
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons( SERVER_PORT );
inet_pton(AF_INET, argv[1], &servaddr.sin_addr); //!> 需要自己写入IP 作为第一个参数
printf("Client connecting...\n");
if( connect(sockfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 )
{
printf("connect error..\n");
exit(EXIT_FAILURE );
}
printf("开始发送data:\n");
str_cli(argv[2], sockfd );
return0;
}
makefile:
all: server client
server:
gcc -W-o server server.c
client:
gcc -W -o client client.c
clean:
rm -f serverclient