在Linux 下開(kāi)發(fā)網(wǎng)絡(luò)通信系統(tǒng)可以充分發(fā)揮Linux 系統(tǒng)出色的網(wǎng)絡(luò)性能据悔,本文介紹了在Linux 操作系統(tǒng)下基于TCP/IP 協(xié)議Socket 套接口的通信機(jī)制以及多線程編程知識(shí)與技巧垂涯,并給出多線程方式實(shí)現(xiàn)多用戶(hù)與服務(wù)端(C/S)并發(fā)通信模型的詳細(xì)算法蚀苛,最后展現(xiàn)了用C 編寫(xiě)的多用戶(hù)與服務(wù)器通信的應(yīng)用實(shí)例并附有運(yùn)行結(jié)果及截圖蚓再。[喝小酒的網(wǎng)摘]http://blog.hehehehehe.cn/a/9145.htm
關(guān)鍵詞:Linux炒辉;套接字;多線程;并發(fā)服務(wù)器;
Socket 是建立在傳輸層協(xié)議(主要是TCP 和UDP)上的一種套接字規(guī)范彻亲,最初由美國(guó)加州Berkley 大學(xué)提出,為UNIX 系統(tǒng)開(kāi)發(fā)的網(wǎng)絡(luò)通信接口吮廉,它定義了兩臺(tái)計(jì)算機(jī)之間通信的規(guī)范苞尝,socket 屏蔽了底層通信軟件和具體操作系統(tǒng)的差異,使得任何兩臺(tái)安裝了TCP 協(xié)議軟件和實(shí)現(xiàn)了Socket 規(guī)范的計(jì)算機(jī)之間的通信成為可能宦芦,Socket 接口是TCP/IP 網(wǎng)絡(luò)最為通用的應(yīng)用接口宙址,也是在Internet 上進(jìn)行網(wǎng)絡(luò)程序應(yīng)用開(kāi)發(fā)最通用的API[1],本文介紹了Socket通信的基本機(jī)制以及采用多線程技術(shù)實(shí)現(xiàn)并發(fā)通信的基本原理调卑,并給出實(shí)例抡砂。
/***************************************************
* 文件名:pthread_server.c
* 文件描述:創(chuàng)建子線程來(lái)接收客戶(hù)端的數(shù)據(jù)
***************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *rec_data(void *fd);
int main(int argc,char *argv[])
{
int server_sockfd;
int *client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
struct sockaddr_in tempaddr;
int i,byte;
char char_recv,char_send;
socklen_t templen;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);//創(chuàng)建套接字
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);//綁定套接字
templen = sizeof(struct sockaddr);
printf("server waiting for connect/n");
while(1){
pthread_t thread;//創(chuàng)建不同的子線程以區(qū)別不同的客戶(hù)端
client_sockfd = (int *)malloc(sizeof(int));
client_len = sizeof(client_address);
*client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, (socklen_t *)&client_len);
if(-1==*client_sockfd){
perror("accept");
continue;
}
if(pthread_create(&thread, NULL, rec_data, client_sockfd)!=0)//創(chuàng)建子線程
{
perror("pthread_create");
break;
}
}
shutdown(*client_sockfd,2);
shutdown(server_sockfd,2);
}
/*****************************************
* 函數(shù)名稱(chēng):rec_data
* 功能描述:接受客戶(hù)端的數(shù)據(jù)
* 參數(shù)列表:fd——連接套接字
* 返回結(jié)果:void
*****************************************/
void *rec_data(void *fd)
{
int client_sockfd;
int i,byte;
char char_recv[100];//存放數(shù)據(jù)
client_sockfd=*((int*)fd);
for(;;)
{
if((byte=recv(client_sockfd,char_recv,100,0))==-1)
{
perror("recv");
exit(EXIT_FAILURE);
}
if(strcmp(char_recv, "exit")==0)//接受到exit時(shí),跳出循環(huán)
break;
printf("receive from client is %s/n",char_recv);//打印收到的數(shù)據(jù)
}
free(fd);
close(client_sockfd);
pthread_exit(NULL);
}
/***************************************************
* 文件名:pthread_client.c
* 文件描述:創(chuàng)建子線程來(lái)接收客戶(hù)端的數(shù)據(jù)
***************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
int i,byte;
char char_send[100] = { 0 };
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))==-1)
{
perror("socket");
exit(EXIT_FAILURE);
}
if(argc != 3){
printf("Usage: fileclient <address> <port>/n");//用法:文件名 服務(wù)器IP地址 服務(wù)器端口地址
return 0;
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("sock");
exit(1);
}
bzero(&address,sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET,argv[1],&address.sin_addr);
len = sizeof(address);
if((result = connect(sockfd, (struct sockaddr *)&address, len))==-1)
{
perror("connect");
exit(EXIT_FAILURE);
}
for(;;)
{
scanf("%s", char_send);//輸入發(fā)送數(shù)據(jù)
fflush(stdin);//清除輸入緩存
if(strcmp(char_send, "exit")==0){//如果輸入exit令野,跳出循環(huán)
if((byte=send(sockfd,char_send,100,0))==-1)
{
perror("send");
exit(EXIT_FAILURE);
}
break;
}
if((byte=send(sockfd,char_send,100,0))==-1)
{
perror("send");
exit(EXIT_FAILURE);
}
}
close(sockfd);
exit(0);
}
編譯服務(wù)器端程序 pthread_server.c :
$gcc pthread_server.c –o server –lpthread
編譯客戶(hù)端程序 pthread_client.c:
$gcc pthread_client.c –o client
編譯在開(kāi)發(fā)板上跑的客戶(hù)端程序:
$arm-linux-gcc client.c –o arm_client
先啟動(dòng)服務(wù)器端的程序 server:
$./server
打開(kāi)另一個(gè)終端舀患,啟動(dòng)客戶(hù)端的程序 client:
$./client 127.0.0.1 9734
把 arm_client 放到開(kāi)發(fā)板上,啟動(dòng) arm_client:
$./arm_client 219.222.170.9 9734
結(jié)果 :
服務(wù)器端:
tongs@tong's-desktop:~/c_c++_program/sock_inet_comm2$ ./server
server waiting for connect
receive from client is client
receive from client is client
receive from client is arm_client
receive from client is arm_client
客戶(hù)端:
tongs@tong's-desktop:~/c_c++_program/sock_inet_comm2$ ./client 127.0.0.1 9734
client
client
exit
開(kāi)發(fā)板服務(wù)器端
[/mnt/yaffs/nfs_share/sock_tcp/thread_socket]./arm_client 219.222.170.9 9734
arm_client
arm_client
exit
[喝小酒的網(wǎng)摘]http://blog.hehehehehe.cn/a/9145.html