谁能给个C语言socket 通信,用UDP协议的代码例子。。 5
使用#include<winsock2.h>库中的socket,学习学习。本人不懂,问题有疑问的话,留言!...
使用#include<winsock2.h>库中的socket,学习学习。本人不懂,问题有疑问的话,留言!
展开
2个回答
展开全部
我的是包含的#include <sys/socket.h>,参考一下吧
<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);
while(1) {
if(fgets(buf, 1024, stdin) == NULL)
break;
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
write(STDOUT_FILENO, buf, n);
}
close(sfd);
return 0;
}
<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1) {
len = sizeof(cli_addr);
n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
for(i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
}
close(sfd);
return 0;
}
请求采纳,谢谢!
<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);
while(1) {
if(fgets(buf, 1024, stdin) == NULL)
break;
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
write(STDOUT_FILENO, buf, n);
}
close(sfd);
return 0;
}
<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1) {
len = sizeof(cli_addr);
n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
for(i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
}
close(sfd);
return 0;
}
请求采纳,谢谢!
推荐于2016-09-15 · 知道合伙人数码行家
关注
展开全部
#include <sys/socket.h>
<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);
while(1) {
if(fgets(buf, 1024, stdin) == NULL)
break;
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
write(STDOUT_FILENO, buf, n);
}
close(sfd);
return 0;
}
<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1) {
len = sizeof(cli_addr);
n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
for(i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
}
close(sfd);
return 0;
}
<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);
while(1) {
if(fgets(buf, 1024, stdin) == NULL)
break;
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
write(STDOUT_FILENO, buf, n);
}
close(sfd);
return 0;
}
<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1) {
len = sizeof(cli_addr);
n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
for(i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
}
close(sfd);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询