Linux下socket 编程模拟C/S服务

48 篇文章 0 订阅

 

C/S  1对1的情况下:

//server.c

/***************************************************************************       
    FileName : server.c   
    FileFunc : 服务器接收到客户端的一个字符,然后对该字符加1后再发给客户端     
    Version  : V0.1        
    Author   : Sunrier        
    Date     : 2012-06-15   
    Descp    : Linux下socket编程模拟C/S服务       
***************************************************************************/ 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])
{
	int server_sockfd,client_sockfd;
	unsigned int server_len,client_len;
	struct sockaddr_in server_address;
	struct sockaddr_in client_address;
	
	unlink("Server_socket"); 
	server_sockfd = socket(AF_INET,SOCK_STREAM,0); /*为服务器创建一个未命名套接字*/
	server_address.sin_family = AF_INET; /*命名套接字*/
	server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
	server_address.sin_port = htons(9876);
	server_len = sizeof(server_address);
	bind(server_sockfd,(struct sockaddr *)&server_address,server_len); /*绑定套接字*/
	listen(server_sockfd,5); /*创建一个连接队列,开始等待客户进行连接 */
	
	while(1)
	{
		char ch;
		printf("Server waiting !\r\n");
		client_len = sizeof(client_address);
		client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,&client_len); /*接收一个客户连接*/
		if( client_sockfd<0 )
			continue;
		read(client_sockfd,&ch,1); /*对套接字上的客户进行读写操作*/
		ch++;
		write(client_sockfd,&ch,1);
		close(client_sockfd);
		/*break;*/
		
	}
	
	return 0;
		
}



 

 

//client.c

/*******************************************************************        
    FileName : client.c 
    FileFunc : 向服务器写入一个字符,然后再从服务器上读取一个字符      
    Version  : V0.1        
    Author   : Sunrier        
    Date     : 2012-06-15   
    Descp    : Linux下socket编程模拟C/S服务    
*******************************************************************/ 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])
{
	int sockfd;
	unsigned int uiLen;
	struct sockaddr_in address;
	int result;
	char ch = 'A';
	sockfd = socket(AF_INET,SOCK_STREAM,0);/*为客户创建一个套接字*/
	address.sin_family = AF_INET;/*命名套接字,与服务器保持一致*/
	address.sin_addr.s_addr = inet_addr("127.0.0.1");
	address.sin_port = htons(9876);
	uiLen = sizeof(address);
	result = connect(sockfd,(struct sockaddr *)&address,uiLen);/*连接服务器的套接字*/
	if( -1==result )
	{
		perror("Test:client1");
		return 1;
	}
	write(sockfd,&ch,1);
	read(sockfd,&ch,1);
	printf("Char from Server = %c !\n",ch);
	return 0;
}




 

 

//makefile

#makefile开始
OBJS = server client
all:$(OBJS)
CFLAGS = -O -Wall -ansi 
CC = gcc $(CFLAGS) 
server:server.c
	@$(CC) -o $@ $?
client:client.c
	@$(CC) -o $@ $?
clean	:
	@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | xargs rm -rf 
#makefile结束


 运行服务器端:

[Sunrier@localhost socket]$ ./server
Server waiting !
Server waiting !
Server waiting !

 

 

运行客户器端:

[root@localhost socket]# ./client
Char from Server = B !
[root@localhost socket]# ./client
Char from Server = B !
[root@localhost socket]#

 

 

 

 

 

C/S  多对1的情况下:

//server.c

/***************************************************************************       
    FileName : server.c   
    FileFunc : 服务器接收到客户端的一个字符,然后对该字符加1后再发给客户端     
    Version  : V0.1        
    Author   : Sunrier        
    Date     : 2012-06-15   
    Descp    : Linux下socket编程模拟C/S服务       
***************************************************************************/ 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])
{
	int server_sockfd,client_sockfd;
	unsigned int server_len,client_len;
	struct sockaddr_in server_address;
	struct sockaddr_in client_address;
	
	unlink("Server_socket"); 
	server_sockfd = socket(AF_INET,SOCK_STREAM,0); /*为服务器创建一个未命名套接字*/
	server_address.sin_family = AF_INET; /*命名套接字*/
	/*server_address.sin_addr.s_addr = inet_addr("127.0.0.1");*/
	server_address.sin_addr.s_addr = htonl(INADDR_ANY);
	server_address.sin_port = htons(9876);
	server_len = sizeof(server_address);
	bind(server_sockfd,(struct sockaddr *)&server_address,server_len); /*绑定套接字*/
	listen(server_sockfd,5); /*创建一个连接队列,开始等待客户进行连接*/
	
	while(1)
	{
		char ch;
		printf("Server waiting !\r\n");
		client_len = sizeof(client_address);
		client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,&client_len); /*接收一个客户连接*/
		if( client_sockfd<0 )
			continue;
		read(client_sockfd,&ch,1); /*对套接字上的客户进行读写操作*/
		ch++;
		write(client_sockfd,&ch,1);
		close(client_sockfd);
		/*break;*/
		
	}
	
	return 0;
		
}



//client1.c

/*******************************************************************        
    FileName : client1.c   
    FileFunc : 客户端1向服务器写入一个字符,然后再从服务器上读取一个字符      
    Version  : V0.1        
    Author   : Sunrier        
    Date     : 2012-06-15   
    Descp    : Linux下socket编程模拟C/S服务    
*******************************************************************/ 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])
{
	int sockfd;
	unsigned int uiLen;
	struct sockaddr_in address;
	int result;
	char ch = 'A';
	sockfd = socket(AF_INET,SOCK_STREAM,0);/*为客户创建一个套接字*/
	address.sin_family = AF_INET;/*命名套接字,与服务器保持一致*/
	address.sin_addr.s_addr = inet_addr("127.0.0.5");
	address.sin_port = htons(9876);
	uiLen = sizeof(address);
	result = connect(sockfd,(struct sockaddr *)&address,uiLen);/*连接服务器的套接字*/
	if( -1==result )
	{
		perror("Test:client1");
		return 1;
	}
	write(sockfd,&ch,1);
	read(sockfd,&ch,1);
	printf("Char from Server = %c !\n",ch);
	return 0;
}



 

//client2.c

/*******************************************************************        
    FileName : client2.c   
    FileFunc : 客户端2向服务器写入一个字符,然后再从服务器上读取一个字符      
    Version  : V0.1        
    Author   : Sunrier        
    Date     : 2012-06-15   
    Descp    : Linux下socket编程模拟C/S服务    
*******************************************************************/ 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])
{
	int sockfd;
	unsigned int uiLen;
	struct sockaddr_in address;
	int result;
	char ch = 'A';
	sockfd = socket(AF_INET,SOCK_STREAM,0);/*为客户创建一个套接字*/
	address.sin_family = AF_INET;/*命名套接字,与服务器保持一致*/
	address.sin_addr.s_addr = inet_addr("127.0.0.6");
	address.sin_port = htons(9876);
	uiLen = sizeof(address);
	result = connect(sockfd,(struct sockaddr *)&address,uiLen);/*连接服务器的套接字*/
	if( -1==result )
	{
		perror("Test:client2");
		return 1;
	}
	write(sockfd,&ch,1);
	read(sockfd,&ch,1);
	printf("Char from Server = %c !\n",ch);
	return 0;
}



 

 

//makefile

#makefile开始
OBJS = server client1 client2
all:$(OBJS)
CFLAGS = -O -Wall -ansi 
CC = gcc $(CFLAGS) 
server:server.c
	@$(CC) -o $@ $?
client1:client1.c
	@$(CC) -o $@ $?
client2:client2.c
	@$(CC) -o $@ $?	
clean	:
	@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | xargs rm -rf 
#makefile结束




 

 运行服务器端:

[Sunrier@localhost socket]$ ./server
Server waiting !
Server waiting !
Server waiting !

 

 

 运行客户器端1:
[root@localhost socket]# ./client1
Char from Server = B !
[root@localhost socket]

 

 运行客户器端2:
[root@localhost socket]# ./client2
Char from Server = B !
[root@localhost socket]

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值