server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "error.h"
#define SERV_PORT 8888
int main(void)
{
int sfd,accprt_fd,i;
struct sockaddr_in addr,client_addr;
socklen_t cl_len;
char client_ip[1024],buf[1024];
addr.sin_family=AF_INET;
addr.sin_port=htons(SERV_PORT);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
sfd = Socket(AF_INET,SOCK_STREAM,0);//创建绑定套接字
Bind(sfd,(struct sockaddr*)&addr,sizeof(addr));
Listen(sfd,128);
cl_len = sizeof(cl_len);
accprt_fd = Accept(sfd,(struct sockaddr*)&client_addr,&cl_len);
printf("client IP:%s port:%d\n",inet_ntop(AF_INET,&client_addr.sin_addr.s_addr,client_ip,sizeof(client_ip)),
ntohs(client_addr.sin_port));
while(1)
{
int n;
n=Read(accprt_fd,buf,sizeof(buf));
write(STDOUT_FILENO,buf,n);
for(i=0;i<n;i++)
{
buf[i] = toupper(buf[i]);
}
Write(accprt_fd,buf,n);
}
Close(sfd);
Close(accprt_fd);
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include "error.h"
#define SERV_PORT 8888
#define SERV_IP "127.0.0.1"
int main(void)
{
int cfd,n;
char buf[1024];
struct sockaddr_in serv_addr;
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,SERV_IP,&serv_addr.sin_addr.s_addr);
cfd = Socket(AF_INET,SOCK_STREAM,0);
Connect(cfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
while(1)
{
fgets(buf,sizeof(buf),stdin);
Write(cfd,buf,strlen(buf));
n = Read(cfd,buf,sizeof(buf));
Write(STDOUT_FILENO,buf,n);
}
Close(cfd);
return 0;
}
error.h
#ifndef _ERROR_H_
#define _ERROR_H_
#include <unistd.h>
#include <sys/socket.h>
void perr_exit(const char* s);
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr);
int Bind(int fd,struct sockaddr *sa,socklen_t salen);
int Socket(int dem,int type,int prototle);
int Listen(int fd,int backlog);
int Connect(int fd,struct sockaddr *sa,socklen_t slen);
ssize_t Read(int fd,void* buf,size_t size);
ssize_t Write(int fd,const void* buf,size_t count);
#endif
error.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
void perr_exit(const char* s)
{
perror(s);
exit(-1);
}
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr)
{
int n;
again:
if((n=accept(fd,sa,salenptr))<0)
{
if((errno==ECONNABORTED)||(errno==EINTR))
goto again;
else
perr_exit("accept error");
}
return n;
}
int Bind(int fd,struct sockaddr *sa,socklen_t salen)
{
int ret;
if((ret=bind(fd,sa,salen))<0)
{
perr_exit("bind error");
exit(-1);
}
return 0;
}
int Socket(int dem,int type,int prototle)
{
int fd;
if((fd=socket(dem,type,prototle))==-1)
{
perr_exit("socket error");
}
return fd;
}
int Listen(int fd,int backlog)
{
int n;
if((n=listen(fd,backlog))<0)
perr_exit("listen error");
return n;
}
int Connect(int fd,struct sockaddr *sa,socklen_t slen)
{
int n;
if((n=connect(fd,sa,slen))<0)
perr_exit("connect error");
return n;
}
ssize_t Read(int fd,void* buf,size_t size)
{
ssize_t n;
again:
if((n=read(fd,buf,size))==-1)
{
if(errno==EINTR)
goto again;
else
return -1;
}
return n;
}
ssize_t Write(int fd,const void* buf,size_t count)
{
ssize_t n;
again:
if((n=write(fd,buf,count))==-1)
{
if(errno==EINTR)
goto again;
else
return -1;
}
return n;
}
int Close(int fd)
{
int n;
if((n=close(fd))<0)
perr_exit("close error");
return n;
}
这是基础的C/S最初模型,添加了必要的出错处理,后期还会书写并发服务器多进程版。现在还在努力学习,如有志同道合之友看到此博客想一同学习的可以加我好友,一起共勉。本人唯一QQ821348195!