一个简单的socket通信例程

/*******tcp_server_p.c**********/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 6666 /*侦听端口地址*/
#define BACKLOG 5 /*侦听队列长度*/


typedef struct Client_Info
{
int client_sc;                    /*保存客户端描述符*/
int clientPort;
char *clientIp;
int socketNum;                   /*指明当前是哪个socket连接*/
struct sockaddr_in *client_addr;
}ClientInfo;


void * thread_newsocket(void *sc_info)
{
time_t tt;  
struct tm tr = {0}; 
ssize_t size = 0;
char buffer[1024]; /*数据的缓冲区*/
// int client_sc;
ClientInfo *client_info = (ClientInfo*)sc_info;
// client_sc = client_info->client_sc; 
printf("--------A New Socket Connect:SocketNum is %d, socketID is %d---------\n", client_info->socketNum, client_info->client_sc);
for(;;){ /*循环处理过程*/
memset(buffer, 0, 1024);
size = read(client_info->client_sc, buffer, 1024); /*从套接字中读取数据放到缓冲区buffer中*/
if(size <= 0){ /*没有数据*/
return;
}
time(&tt);
printf("Socket%d have new data, Receive time is %s", client_info->socketNum, ctime(&tt));
printf("Client IP is %s, Port is %d, ",  client_info->clientIp, client_info->clientPort);
printf("data length is %d, data value is %s\n", size, buffer);
//fflush(stdout);
/*构建响应字符,为接收到客户端字节的数量*/
sprintf(buffer, "Server have received %d bytes data\n", size);
write(client_info->client_sc, buffer, strlen(buffer)+1);/*发给客户端*/
}
pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
int ss,sc; /*ss为服务器的socket描述符,sc为客户端的socket描述符*/
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int err; /*返回值*/
// pthread_t tid[20];
int socket_num = 0;
pthread_t tid[100];
ClientInfo *pClientInfo[100];
// pClientInfo = (Client_Info*)malloc(sizeof(Client_Info));
/*建立一个流式套接字*/
ss = socket(AF_INET, SOCK_STREAM, 0);
if(ss < 0){
perror("socket error");
return -1;
}

/*设置服务器地址*/
bzero(&server_addr, sizeof(server_addr)); /*清零*/
server_addr.sin_family = AF_INET; /*协议族*/
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*本地地址*/
server_addr.sin_port = htons(PORT); /*服务器端口*/

/*绑定地址结构到套接字描述符*/
err = bind(ss, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(err < 0){/*出错*/
perror("bind error");
return -1;
}

/*设置侦听*/
err = listen(ss, BACKLOG);
if(err < 0){ /*出错*/
perror("listen error");
return -1;
}

/*主循环过程*/
for(;;) {

socklen_t addrlen = sizeof(struct sockaddr);
sc = accept(ss, (struct sockaddr*)&client_addr, &addrlen); 
/*接收客户端连接*/
if(sc < 0){
continue;
}
pClientInfo[socket_num] = (ClientInfo*)malloc(sizeof(ClientInfo));
pClientInfo[socket_num]->client_sc = sc;
pClientInfo[socket_num]->clientPort = client_addr.sin_port;
pClientInfo[socket_num]->clientIp = inet_ntoa(client_addr.sin_addr);
pClientInfo[socket_num]->socketNum = socket_num;

socket_num++;
printf("sc is %d, socketNum is %d", pClientInfo[socket_num - 1]->client_sc, pClientInfo[socket_num - 1]->socketNum);
err = pthread_create(&tid[socket_num - 1], NULL, &thread_newsocket, pClientInfo[socket_num - 1]);
if(err != 0)
{
perror("");
}
else
printf("tid is %d\n", tid);
}
}

 
  

/*******tcp_client_p.c**********/
#include
#include
#include
#include
#include
#include
#include
#define PORT 6666 /*侦听端口地址*/


/*客户端的处理过程*/
void process_conn_client(int s)
{
ssize_t size = 0;
char buffer[1024]; /*数据的缓冲区*/

for(;;){ /*循环处理过程*/
/*从标准输入中读取数据放到缓冲区buffer中*/
size = read(0, buffer, 1024);
if(size > 0){ /*读到数据*/
write(s, buffer, size); /*发送给服务器*/
size = read(s, buffer, 1024); /*从服务器读取数据*/
write(1, buffer, size); /*写到标准输出*/
}
}
}


int main(int argc, char *argv[])
{
int socket_c = -1; /*socket_c为socket描述符*/
struct sockaddr_in server_addr; /*服务器地址结构*/
int rv = 0;
socket_c = socket(AF_INET, SOCK_STREAM, 0); /*建立一个流式套接字 */
if(socket_c < 0){ /*出错*/
perror("socket error");
return -1;
}

/*设置服务器地址*/
bzero(&server_addr, sizeof(server_addr)); /*清零*/
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*本地地址*/
server_addr.sin_port = htons(PORT); /*服务器端口*/
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
/*连接服务器*/
rv = connect(socket_c, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
if(rv != 0)
{
perror("connect error");
return -1;
}
process_conn_client(socket_c); /*客户端处理过程*/
close(socket_c); /*关闭连接*/
return 0;
}

你可能感兴趣的:(一个简单的socket通信例程)