服务器
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#define IP "192.168.31.51"
#define PORT 9119
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
int sfd,newfd;
struct sockaddr_in my_addr,peer_addr;
//创建套接字:socket
sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速被重复使用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = inet_addr(IP);
//绑定服务器的IP和端口:必须绑定:bind
if(bind(sfd,(struct sockaddr*)&my_addr,sizeof(my_addr)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//将套接字设置为被动监听状态:lieten
if (listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
socklen_t addrlen = sizeof(peer_addr);
//获取连接成功后的套接字:accept
newfd = accept(sfd, (struct sockaddr *) &peer_addr,&addrlen);
if (newfd == -1)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d 连接成功__%d__\n",\
inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port),newfd,__LINE__);
//接受客户端的信息:recv
ssize_t res = 0;
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s : %d] newfd = %d 客户端下线__%d__\n",\
inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port),newfd,__LINE__);
}
printf("[%s : %d] newfd = %d : %s__%d__\n",\
inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port),newfd,buf,__LINE__);
//发送给客户端:send
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
//关闭套接字:close
close(sfd);
close(newfd);
return 0;
}
客户端
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.51" //ifconfig出来的本机IP
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//绑定---》非必须绑定
//功能:将客户端的IP地址和端口号绑定到客户端套接字文件
//如果不绑定,则操作系统会自动给客户端绑定本机IP和随机端口
//填充服务器的地址信息结构体,该结构体用于给connect函数使用
//该真实结构体根据地址族限定:AF_INET;
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填充AF_INET
sin.sin_port = htons(9119); //端口号的网络字节序1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //IP,本机IP ifconfig
//连接服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//对于字符串操作之前或者操作之后要清空,
//防止之前的数据对这次操作有干扰
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送
if(send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
//对于字符串操作之前或者操作之后要清空,
//防止之前的数据对这次操作有干扰
bzero(buf,sizeof(buf));
//接收
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线\n");
break;
}
printf( ": %s__%d__\n",buf,__LINE__);
}
//关闭文件描述符
close(cfd);