#include
#include
#include
#include
#include
#include
#include
#define PORT 8880
#define IP "192.168.114.188"
#define ERR_MSG(msg) do\
{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d\n",sfd);
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速的被复用成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,128) <0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
int newfd1= -1;
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
newfd1=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd1<0)
{
ERR_MSG("newfd1");
return -1;
}
printf("[%s:%d]客户端连接成功 newfd1=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd1);
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
int res=recv(newfd1,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}else if(0==res)
{
printf("[%s:%d]客户端下线 newfd1=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd1);
break;
}
printf("[%s:%d] newfd1=%d : %s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd1,buf);
strcat(buf,"*_*");
if(send(newfd1,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
}
close(newfd1);
if(close(sfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
#include
#define PORT 8880
#define IP "192.168.114.188"
#define ERR_MSG(msg) do\
{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d\n",cfd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
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;
}
bzero(buf,sizeof(buf));
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}else if(0==res)
{
printf("服务器下线 cfd=%d\n",cfd);
break;
}
printf("cfd=%d : %s\n",cfd,buf);
}
if(close(cfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
#include "myhead.h"
#define ERR_MSG(msg) do{\
printf("__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8880
#define IP "192.168.114.188"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d\n",sfd);
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128]="";
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen)<0)
{
ERR_MSG("recv");
return -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
strcat(buf,":)");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
close(sfd);
return 0;
}
#include "myhead.h"
#define ERR_MSG(msg) do{\
printf("__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8880
#define IP "192.168.114.188"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d\n",cfd);
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
char buf[128]="";
ssize_t res=0;
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
strcat(buf,":)");
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
bzero(buf,sizeof(buf));
if((res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen))<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("服务器下线 cfd=%d\n",cfd);
break;
}
printf("cfd=%d:%s\n",cfd,buf);
}
if(close(cfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}