您的位置:首页 > 编程语言

一个简单的echo服务模型(最原始版本,代码没有优化)

2011-06-21 19:57 537 查看
服务器端:

#include <stdio.h>

#include <stdlib.h>

#include <strings.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <error.h>

#define PORT 8888

#define BACKLOG 5

void process_conn_server(int s);

int main()

{

int ss,sc; /*the socket descriptor of the server and the client*/

struct sockaddr_in server_addr;

struct sockaddr_in client_addr;

int err; /*the return value*/

pid_t pid;

/*create a stream socket*/

ss = socket(AF_INET,SOCK_STREAM,0);

/*check the return value*/

if(ss < 0)

{

perror("socket error:");

return -1;

}

/*set the struct of the address of the server*/

bzero(&server_addr,sizeof(server_addr)); /*clean*/

server_addr.sin_family = AF_INET;

server_addr.port = htons(PORT);

server_addr.sin_addr.s_addr = htol(INADDR_ANY);

/*bind the socket to the addrs*/

err = bind(ss,(struct sockaddr*)&server_addr,

sizeof(server_addr));

if(err < 0)/*check the return value*/

{

perror("bind error:");

return -1;

}

/*set listen*/

err = listen(ss,BACKLOG);

if(err < 0)

{

perror("listen error:");

return -1;

}

for(;;)

{

int addrlen = sizeof(struct sockaddr);

/*accept a connection and create a new socket descriptor*/

sc = accept(ss,(struct sockaddr*)&client_addr,&addrlen);

if(sc <0)

{

continue;

}

/*create a new process to deal with the new connect*/

pid = fork();

if(pid == 0) /*this is the child process*/

{

close(ss);

process_conn_server(sc); /*the process function*/

}

else /*this is the parent process*/

{

close(sc);

}

}

/*the defination of the process function*/

void process_conn_server(int s)

{

ssize_t size = 0;

char buffer[1024];

for(;;)

{

size = read(s,buffer,1024);

if(size == 0)

{

return ;

}

sprintf(buffer,"%d bytes altogether/n",size);

write(s,buffer,strlen(buffer)+1);/*send to the client*/

}

}

}

客户端:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <error.h>
#include <error.h>
#define PORT 8888
void process_conn_client(int s);
int main()
{
int s; /*the socket des*/
struct sockaddr_in server_addr;
int err; /*the return value*/
char server_ip[1024];
/*get the server ip address*/
printf("please input the servr ip:");
scanf("%s",server_ip);

s = socket(AF_INET,SOCK_STREAM,0);
if(s < 0)
{
perror("socket error:");
return -1;
}

/*set the address of the server*/
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addf.s_addr = htonl(INADDR_ANY);

inet_pton(AF_INET,server_ip,&server_addr.sin_addr);
/*connect to the server*/
connect(s,(struct sockaddr*)&server_addr,
sizeof(struct sockaddr));
process_conn_clients(s);
close(s);

}

/*the defination of the process function*/
void process_conn_client(int s)
{
ssize_t size = 0;
char buffer[1024];

for(;;)
{
size = read(0,buffer,1024);
if(size > 0)
{
write(s,buffer,size);
size = read(s,buffer,1024);
write(1,buffer,size);
}
}
}

另外一个echo,到现在为止还没弄好,也不知道毛病在哪。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐