原文:Linux 下 简单客户端服务器通讯模型(TCP)

服务器端:server.c

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h> #define ServerPort 6080
#define MaxUser 8 typedef struct _server{ int sock_fd; int new_fd; struct sockaddr_in my_addr; } Server; typedef struct _client{ int sock_fd; struct sockaddr_in my_addr; } Client; static Server *server; static Client *client; void socket_server_init(Server * server){ server -> my_addr.sin_family = AF_INET; server -> my_addr.sin_port = htons(ServerPort); server -> my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server -> my_addr.sin_zero),); } int socket_server_create_bind_listen(Server * server){ int sin_size,ret, on; if((server -> sock_fd = socket(AF_INET,SOCK_STREAM,)) == -){ perror("faild to create socket :\n"); return -;
}
on = ; ret = setsockopt( server -> sock_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ); if(ret=bind(server -> sock_fd,
(struct sockaddr *)&server -> my_addr,
sizeof(struct sockaddr))==-){ perror("failed to bind \n"); return -; }
if(listen(server -> sock_fd,MaxUser)==-){ perror("failed to listen \n"); return -;
} return ;
} void server_send_msg(int fd){ char buff[]; while(){ fgets(buff,,stdin); if(send(fd,buff,,)==-){ perror("error to send :"); break;
} } } void server_wait_service(Server *server,Client *client){ int new_fd ,ret,sin_size; char buf[]; pthread_t tid; sin_size = sizeof(client ->my_addr); new_fd = accept(server ->sock_fd,(struct sockaddr *)&client->my_addr,&sin_size); if(new_fd <= ){ perror("accept error :"); }else{ ret = pthread_create(&tid,NULL,server_send_msg,new_fd); if(ret!=){ perror("error to create pthread"); }
while(){
if(recv(new_fd,buf,sizeof(buf),)>){ printf("\nclient said:\t\t%s\n",buf); }
}
}
} int main(int arg, char * argv[]){ server = (Server *)malloc(sizeof(Server)); client = (Client *)malloc(sizeof(Client)); socket_server_init(server); if(socket_server_create_bind_listen(server)){ server_wait_service(server,client); } return ; }

客户端代码:client.c

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include <stdio.h>
#include <netdb.h> #define ServerPort 6080 typedef struct _server{ int sock_fd; int new_fd; struct sockaddr_in my_addr; } Server; typedef struct _client{ int sock_fd; struct sockaddr_in my_addr; } Client; Server *server; Client *client; void socket_server_init(Server * server){ server -> my_addr.sin_family = AF_INET; server -> my_addr.sin_port = htons(ServerPort); server -> my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server -> my_addr.sin_zero),); } void socket_client_init(Client * client){ client -> my_addr.sin_family = AF_INET; client -> my_addr.sin_port = htons(ServerPort+); client -> my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(client -> my_addr.sin_zero),); } int socket_client_create_connect(Server * server,Client * client){ if((client -> sock_fd = socket(AF_INET,SOCK_STREAM,)) == -){ perror("failed to create client socket \n"); return -; } else if(connect(client ->sock_fd,
(struct sockaddr *)&server ->my_addr,
sizeof(server ->my_addr))==-){ printf("failed to connect the server \n"); return -; }else return ; } void client_recv_msg(Client *client){ char buf[]; while(){ if(recv(client->sock_fd,buf,sizeof(buf),)>){ printf("\nserver said :\t\t%s\n",buf); }else break;
} } int main(int argc,char* argv[]){ server = (Server *)malloc(sizeof(Server)); client = (Client *)malloc(sizeof(Client)); char data[]; socket_server_init(server); socket_client_init(client); pthread_t tid; if(socket_client_create_connect(server,client)){ pthread_create(&tid, NULL,client_recv_msg, client); while(){ fgets(data,,stdin); if(send(client->sock_fd,data,,)==-){ perror("error to send :"); break;
}
}
} return ;
}

Makefile:

all:    server client

server:    server.c

    gcc -o server server.c -pthread

client:    client.c

    gcc -o client client.c -pthread

clean:
rm -f server client

执行效果:

server:

root@ubuntu:/mnt/hgfs/E/Lessons/个人总结/chat# ./server

client said: hello

hehe

client said: g

client said: goodbay

client:

root@ubuntu:/mnt/hgfs/E/Lessons/个人总结/chat# ./client
hello

server said : hehe

s^Hg
goodbay


去年的代码,做个备份。

05-02 21:42