server.c
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<errno.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<arpa/inet.h>
#define _PORT_ 9999
#define _BACKLOG_ 10
int main()
{
int sock = socket (AF_INET, SOCK_STREAM, 0); //SOCK_STREAM TCP协议 面向字节流 AF_INET 表示IPv4 addresss.
if( sock<0 )
{
printf("creat socket error ,errno is :%d, errstring is :%s",errno, strerror(errno));
return 1;
}
struct sockaddr_in server_socket,client_socket;
bzero(&server_socket, sizeof(server_socket)); //初始化server_socket结构体。
server_socket.sin_family = AF_INET;
server_socket.sin_addr.s_addr = htonl(INADDR_ANY); // INADDR_ANY is meaning any of my IP address. Its value can be get by system.
server_socket.sin_port = htons(_PORT_);
if(bind(sock, (struct sockaddr*)&server_socket, sizeof(server_socket) )< 0)
{
printf("bind error, error code is %d, error string is :%s \n",errno, strerror(errno) );
close(sock);
return 2;
}
if(listen(sock,_BACKLOG_)<0)
{
printf("listen error, error code is:%d , error string is : %s \n", errno, strerror(errno));
return 3;
}
//socket 与 address 和 port 完成绑定。
printf("bind and listen success, wait accept ~ ~ ~\n");
while(1)
{
socklen_t len=sizeof(client_socket);
int client_sock= accept(sock, (struct sockaddr *)&client_socket, &len); // 创建了新的socket文件 来接受客户的信息
if(client_sock<0)
{
printf("accept error, errno is :%d, errstring is :%s \n", errno ,strerror(errno));
close(sock);
return 4;
}
// 链接成功 打印客户端的 IP 和 Port
char buf_ip[INET_ADDRSTRLEN];
memset(buf_ip, '\0', sizeof(buf_ip));
inet_ntop(AF_INET, &client_socket.sin_addr, buf_ip, sizeof(buf_ip)); //对客户的地址进行提取
printf("get connert, ip is :%s port is :%d \n", buf_ip, ntohs(client_socket.sin_port) );
// 单用户版本 以下为对用户的服务
while(1)
{
char buf[1024]; // 定义缓冲区
memset(buf,'\0', sizeof(buf)); // 清零缓冲区
read(client_sock,buf, sizeof(buf)); //读取用户需求
printf("client:# %s\n",buf); //打印用户发来的消息
memset(buf, '\0',sizeof(buf)); //再次清零缓冲区
printf("server:$ ");
fgets(buf,sizeof(buf),stdin); //输入信息到buf
buf[strlen(buf)-1]='\0'; //将末尾的 \n 修改为 \0
write(client_sock, buf, strlen(buf)+1); //将内容写入client_sock
printf("Massage send to client please wait ......\n");
}
//与用户持续交流
}
close(sock);
return 0;
}
client.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<strings.h>
#include<errno.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define SERVER_PORT 8080
#define SERVER_IP "127.0.0.1"
int main()
{
char buf[1024];
memset(buf,'\0', sizeof(buf));
struct sockaddr_in server_sock;
int sock=socket(AF_INET, SOCK_STREAM, 0);
bzero(&server_sock, sizeof(server_sock));
server_sock.sin_family=AF_INET;
inet_pton(AF_INET,SERVER_IP,&server_sock.sin_addr);
server_sock.sin_port= htons(SERVER_PORT);
int ret = connect(sock,(struct sockaddr *)&server_sock,sizeof(server_sock));
if(ret<0)
{
printf("connerct failed..., errno is :%d, errstring is :%s", errno, strerror(errno));
return 1;
}
printf("connerct success ~ ~ ~\n");
while(1)
{
printf("client: ");
fgets(buf, sizeof(buf),stdin);
buf[strlen(buf)-1] ='\0';
write(sock, buf, sizeof(buf));
if(strncasecmp(buf,"quit",4)==0)
{
printf("quit!\n");
break;
}
printf("please wait ...... \n");
read(sock, buf, sizeof(buf));
printf("server:%s\n", buf);
}
close(sock);
return 0;
}