服务器
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define IP "127.0.0.1" //本机IP
#define ERR_MSG(msg) do{\
fprintf(stderr,"Line : %d\n",__LINE__);\
perror("msg");\
}while(0) //判断是否出错
int main(int argc, const char *argv[])
{
//创建套接字
int server_fd = socket(AF_INET,SOCK_STREAM,0);
if(server_fd < 0)
{
ERR_MSG("socket");
return -1;
}
//使端口能够被快速重复使用
int resuse = 1;
if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&resuse,sizeof(resuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速使用\n");
//填充服务器的地址信息结构体
#if 0
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
#endif
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(7777); //端口号
sin.sin_addr.s_addr = inet_addr(IP); //本机IP
//绑定服务器的IP和端口 必须要绑定的
if(bind(server_fd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//监听
if(listen(server_fd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//存储客户端信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int client_fd = accept(server_fd,(struct sockaddr*)&cin,&addrlen);
if(client_fd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] client_fd = %d 连接成功__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),client_fd,__LINE__);
char ch[128] = "";
ssize_t res = 0;
while(1)
{
bzero(ch,sizeof(ch));
res = recv(client_fd,ch,sizeof(ch),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res == 0)
{
printf("[%s : %d] client_fd = %d 客户端下线__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),client_fd,__LINE__);
break;
}
printf("[%s : %d] client_fd = %d : %s__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),client_fd,ch,__LINE__);
//发送
strcat(ch,"*_*");
if(send(client_fd,ch,sizeof(ch),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
//关闭文件描述符
close(server_fd);
close(client_fd);
return 0;
}
客户端
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define IP "127.0.0.1" //本机IP
#define ERR_MSG(msg) do{\
fprintf(stderr,"Line : %d\n",__LINE__);\
perror("msg");\
}while(0) //判断是否出错
int main(int argc, const char *argv[])
{
//创建套接字
int client_fd = socket(AF_INET,SOCK_STREAM,0);
if(client_fd < 0)
{
ERR_MSG("socket");
return -1;
}
/*
//使端口能够被快速重复使用
int resuse = 1;
if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&resuse,sizeof(resuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速使用\n");
//填充服务器的地址信息结构体*/
#if 0
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
#endif
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(7777); //端口号
sin.sin_addr.s_addr = inet_addr(IP); //本机IP
/*
//绑定服务器的IP和端口 必须要绑定的
if(bind(server_fd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//监听
if(listen(server_fd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//存储客户端信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int client_fd = accept(server_fd,(struct sockaddr*)&cin,&addrlen);
if(client_fd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] client_fd = %d 连接成功__%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),client_fd,__LINE__);
*/
if(connect(client_fd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char ch[128] = "";
ssize_t res = 0;
while(1)
{
bzero(ch,sizeof(ch));
printf("请输入--->>>>>");
fgets(ch,sizeof(ch),stdin);
ch[strlen(ch)-1] = '\0' ;
if(send(client_fd,ch,sizeof(ch),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send successful\n");
bzero(ch,sizeof(ch));
res = recv(client_fd,ch,sizeof(ch),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res == 0)
{
printf("服务器下线\n");
break;
}
printf(": %s__%d__\n",ch,__LINE__);
}
//关闭文件描述符
close(client_fd);
return 0;
}
客户端连接服务器