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),8);
}
int socket_server_create_bind_listen(Server * server){
int sin_size,ret, on;
if((server -> sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("faild to create socket :\n");
return -1;
}
on = 1;
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))==-1){
perror("failed to bind \n");
return -1;
}
if(listen(server -> sock_fd,MaxUser)==-1){
perror("failed to listen \n");
return -1;
}
return 1;
}
void server_send_msg(int fd){
char buff[1024];
while(1){
fgets(buff,1024,stdin);
if(send(fd,buff,1024,0)==-1){
perror("error to send :");
break;
}
}
}
void server_wait_service(Server *server,Client *client){
int new_fd ,ret,sin_size;
char buf[1024];
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 <= 0 ){
perror("accept error :");
}else{
ret = pthread_create(&tid,NULL,server_send_msg,new_fd);
if(ret!=0){
perror("error to create pthread");
}
while(1){
if(recv(new_fd,buf,sizeof(buf),0)>0){
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 0;
}
客户端代码: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),8);
}
void socket_client_init(Client * client){
client -> my_addr.sin_family = AF_INET;
client -> my_addr.sin_port = htons(ServerPort+10);
client -> my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(client -> my_addr.sin_zero),8);
}
int socket_client_create_connect(Server * server,Client * client){
if((client -> sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("failed to create client socket \n");
return -1;
}
else if(connect(client ->sock_fd,
(struct sockaddr *)&server ->my_addr,
sizeof(server ->my_addr))==-1){
printf("failed to connect the server \n");
return -1;
}else
return 1;
}
void client_recv_msg(Client *client){
char buf[1024];
while(1){
if(recv(client->sock_fd,buf,sizeof(buf),0)>0){
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[1024];
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(1){
fgets(data,1024,stdin);
if(send(client->sock_fd,data,1024,0)==-1){
perror("error to send :");
break;
}
}
}
return 0;
}
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
去年的代码,做个备份。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。