网络程序之UDP通讯
来源:互联网 发布:淘宝二手cpu推荐店铺 编辑:程序博客网 时间:2024/04/29 19:43
网络程序之UDP通讯
#define MYPORT 5050// 监听端口
void main()
{
SOCKET网络程序设计中UDP是经常要用到的技术.因为它速度快,不面向连接.用它来传送数据在局域网中相对于TCP有很大的优势.这里对于UDP协议我不想做介绍了.给出一个UDP通讯程序的源代码.用它来实现一个点对点通讯工具.
client.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 5050 /* Server的端口 */
#define MAXDATASIZE 1000 /*一次可以读的最大字节数 */
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 5050 /* Server的端口 */
#define MAXDATASIZE 1000 /*一次可以读的最大字节数 */
int main( )
{
int sockfd, numbytes, sin_size;
char buf[MAXDATASIZE] = “Hello, world!”;
struct hostent *he; /* 主机信息 */
struct sockaddr_in their_addr; /* 对方地址信息 */
/* get the host info */
if ((he=gethostbyname("localhost")) == NULL) {
herror("gethostbyname");
exit(1);
}
if ((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
perror("socket");
exit(1);
}
{
int sockfd, numbytes, sin_size;
char buf[MAXDATASIZE] = “Hello, world!”;
struct hostent *he; /* 主机信息 */
struct sockaddr_in their_addr; /* 对方地址信息 */
/* get the host info */
if ((he=gethostbyname("localhost")) == NULL) {
herror("gethostbyname");
exit(1);
}
if ((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT); /* short, NBO */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* 其余部分设成0 */
numbytes = sendto(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&their_addr,sizeof(their_addr));
if (numbytes == -1) {
perror(“sendto");
exit(1);
}
printf(“Send: %s",buf);
numbytes = recvfrom(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&their_addr, &sin_size);
if (numbytes == -1) {
perror("recvfrom");
exit(1);
}
buf[numbytes] = '/0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}
their_addr.sin_port = htons(PORT); /* short, NBO */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* 其余部分设成0 */
numbytes = sendto(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&their_addr,sizeof(their_addr));
if (numbytes == -1) {
perror(“sendto");
exit(1);
}
printf(“Send: %s",buf);
numbytes = recvfrom(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&their_addr, &sin_size);
if (numbytes == -1) {
perror("recvfrom");
exit(1);
}
buf[numbytes] = '/0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}
server.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
//include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
//include <sys/wait.h>
#define MYPORT 5050// 监听端口
void main()
{
int sockfd; // 数据端口
struct sockaddr_in my_addr; // 自身的地址信息
struct sockaddr_in their_addr; // 连接对方的地址信息
int sin_size, retval;
char buf[128];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // 网络字节顺序
my_addr.sin_addr.s_addr = INADDR_ANY; // 自动填本机IP
bzero(&(my_addr.sin_zero), 8); // 其余部分置0
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
perror("bind");
return;
struct sockaddr_in my_addr; // 自身的地址信息
struct sockaddr_in their_addr; // 连接对方的地址信息
int sin_size, retval;
char buf[128];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // 网络字节顺序
my_addr.sin_addr.s_addr = INADDR_ANY; // 自动填本机IP
bzero(&(my_addr.sin_zero), 8); // 其余部分置0
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
perror("bind");
return;
}
// 主循环
while(1) {
retval = recvfrom(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, &sin_size);
printf("Received datagram from %s/n",inet_ntoa(their_addr.sin_addr));
if (retval == 0) {
perror ("recvfrom");
close(sockfd);
break;
// 主循环
while(1) {
retval = recvfrom(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, &sin_size);
printf("Received datagram from %s/n",inet_ntoa(their_addr.sin_addr));
if (retval == 0) {
perror ("recvfrom");
close(sockfd);
break;
}
retval = sendto(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, sin_size);
}
retval = sendto(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, sin_size);
}
}
- 网络程序之UDP通讯
- UDP 网络广播通讯
- SockDemo UDP 通讯程序
- Java Udp通讯程序
- udp通讯程序多线程
- UDP通讯小程序
- 网络编程之UDP网络程序
- [学习笔记]Java网络编程之UDP通讯
- [学习笔记]Java网络编程之UDP通讯
- Java 网络编程之 (UDP网络聊天程序)
- UDP简单通讯之二
- Qt之局域网UDP通讯
- Android之UDP协议通讯
- UDP网络程序-1
- UDP网络程序
- UDP网络程序
- Java UDP通讯程序升级版
- java中的简单Udp通讯程序
- 网络程序之端口扫描技术
- 网络程序之Http Server的实现
- 技术blog - 好的开始是成功的一半
- 设计器动词
- 网络程序之ping指令的实现
- 网络程序之UDP通讯
- java对数据库的操作
- java网络编程之HTTP服务器的实现
- java网络编程之聊天室客户端(二)
- 神州数码组织级项目管理发展历程
- 聊天室服务器端软件源代码
- 用Microsoft SQL和数据库进行连接时要注意的问题:
- hibernate领袖看软件设计与开发管理
- 通过win32api让c#控制Windows任务栏