网络编程第四天
2023-12-26 20:17:05
tcp套接字客户端
#include "../head.h"
#define IP "192.168.124.29"
#define PORT 8888
struct mallage
{
int newfd;
struct sockaddr_in cin;
};
void *callBack(void *arg)
{ char buf[128] = "";
ssize_t res = 0;
int newfd = ((struct mallage*)arg)->newfd;
struct sockaddr_in cin = ((struct mallage*)arg)->cin;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
break;
}
else if(0 == res)
{
printf("newfd=%d 客户已下线 __%d__\n",newfd,__LINE__);
printf("%s : %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
break;
}
printf("%s : %d 说:",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
printf("%s\n",buf);
LINE;
strcat(buf,"^_^");
if(send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
break;
}
printf("send success");
LINE;
}
close(newfd);
pthread_exit(NULL);
}
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 __%d__\n",sfd,__LINE__);
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
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 __%d__\n",__LINE__);
if(listen(sfd,128) < 0)
{
ERR_MSG("lsiten");
return -1;
}
printf(" listen success __%d__\n",__LINE__);
struct mallage info;
socklen_t addrlen = sizeof(info.cin);
pthread_t tid;
while(1)
{
info.newfd = accept(sfd,(struct sockaddr*)&info.cin,&addrlen);
if(info.newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd=%d 客户端连接成功 __%d__\n",info.newfd,__LINE__);
printf("%s : %d\n",inet_ntoa(info.cin.sin_addr),ntohs(info.cin.sin_port));
if(pthread_create(&tid,NULL,callBack,(void*)&info) != 0)
{
fprintf(stderr,"pthread_create faild__%d__\n",__LINE__);
return -1;
}
pthread_detach(tid);
}
if(close(sfd) < 0 )
{
ERR_MSG("close");
return -1;
}
return 0;
}
udp套接字服务器
#include "../head.h"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
if(access("./Udpunix",F_OK) == 0)
{
unlink("./Udpunix");
}
struct sockaddr_un sun;
sun.sun_family =AF_UNIX;
strcpy(sun.sun_path,"./Udpunix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
struct sockaddr_un cun;
socklen_t addrlen = sizeof(cun);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("%s\n",buf);
LINE;
printf("请回复上帝>>>>");
fgets(buf,sizeof(buf),stdin);
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,sizeof(cun)) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success");
LINE;
}
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
文章来源:https://blog.csdn.net/hahhsssds/article/details/135228573
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!