socket也即套接字接口,实际上也就是网络上的通信节点。使用者或者应用程序只要连接到socket便可以和网络上任何一个通信端点连接,传递数据。LINUX中,程序员可以不必关心通信协议而专注应用程序开发。根据数据传送方式,可以把socket分成面向连接的数据流通信和无连接的数据报通信。通信过程如下:
函数实例:
A,服务器端程序echo_serv.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#define EHCO_PORT 8080
#define MAX_CLIENT_NUM 10
void * memsets (void *s, int c, int count)
{
char *xs = s;
while (count--)
*xs++ = c;
return s;
}
#define bzero(b, len) memsets((b), '\0', (len))
int main()
{
int sock_fd;
struct sockaddr_in serv_addr;
int clientfd;
struct sockaddr_in clientAddr;
char buff[101];
socklen_t len;
int closing = 0;
int n;
/*create socket*/
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd == -1){
printf("create socket error! \r\n");
return 0;
}else{
printf("succress to create socket %d \r\n",sock_fd);
}
/*setting server addr*/
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(EHCO_PORT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY); //INADDR_ANY表示可以跟任何主机通信
bzero(&(serv_addr.sin_zero), 8);
/*bind addr with soket*/
if(bind(sock_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))!=0){
printf("bind address fail! %d \r\n",errno);
close(sock_fd);
return 0;
}else{
printf("success to bind address \r\n");
}
/*setting listener*/
if(listen(sock_fd,MAX_CLIENT_NUM)!=0){
printf("listen socket error \r\n");
close(sock_fd);
return 0;
}else{
printf("success to bind listen \r\n");
}
/*create another socket*/
len = sizeof(clientAddr);
clientfd = accept(sock_fd,(struct sockaddr*)&clientAddr,&len); //获得连接上的客户端的socket
if(clientfd<=0){
printf("accept error !\r\n");
close(sock_fd);
return 0;
}
/*get data from client user*/
while((n = recv(clientfd,buff,100,0))>0){
buff[n] = '\0';
printf("number of receive bytes = %d data = %s \n",n,buff);
fflush(stdout);
send(clientfd,buff,n,0);
if(strncmp(buff,"quit",4) == 0)
break;
}
close(clientfd);
close(sock_fd);
return 0;
}
B,客户端程序 echo_client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#define EHCO_PORT 8080
#define MAX_COMMAND 5
void * memsets (void *s, int c, int count)
{
char *xs = s;
while (count--)
*xs++ = c;
return s;
}
#define bzero(b, len) memsets((b), '\0', (len))
int main()
{
int sock_fd;
struct sockaddr_in serv_addr;
char *buff[MAX_COMMAND] = {"abc","def","test","hello","quit"};
char tmp_buf[100];
socklen_t len;
int n,i;
/*create socket*/
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd == -1){
printf("create socket error! \r\n");
return 0;
}else{
printf("succress to create socket %d \r\n",sock_fd);
}
/*setting server addr*/
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(EHCO_PORT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY); //INADDR_ANY表示可以跟任何主机通信
bzero(&(serv_addr.sin_zero), 8);
/*connect server*/
if(connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1){
printf("connect error \r\n");
close(sock_fd);
return 0;
}
printf("success connect to server \r\n");
/*send and receive data*/
for(i=0;i<MAX_COMMAND;i++){
send(sock_fd,buff[i],100,0);
n = recv(sock_fd,tmp_buf,100,0);
tmp_buf[n] = '\0';
printf("data send :%s receive :%s \n",buff[i],tmp_buf);
if(0==strncmp(tmp_buf,"quit",4))
break;
}
close(sock_fd);
return 0;
}
分别使用不同的终端用gcc编译这两个程序并执行,得到通信结果。
服务器端结果客户端结果