歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux 網絡編程:多客戶請求服務器( C/S )實例

Linux 網絡編程:多客戶請求服務器( C/S )實例

日期:2017/3/1 10:26:02   编辑:Linux編程
注意:可以開啟一個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
Copyright © Linux教程網 All Rights Reserved