当前位置: 技术问答>linux和unix
一个简单UDP的程序,客户端发送消息后,服务器端正常接受,但是服务器端发不出消息?
来源: 互联网 发布时间:2015-09-12
本文导语: //server端 头文件unp.h为 unix环境高级编程里的头文件 #include "unp.h" #define MAX_MSG_SIZE 10 void udps_respon(int sockfd) { struct sockaddr addr; struct sockaddr_in* p_addr; int n; socklen_t addrlen = 0; char msg[MAX_MSG_SI...
//server端
头文件unp.h为 unix环境高级编程里的头文件
#include "unp.h"
#define MAX_MSG_SIZE 10
void udps_respon(int sockfd)
{
struct sockaddr addr;
struct sockaddr_in* p_addr;
int n;
socklen_t addrlen = 0;
char msg[MAX_MSG_SIZE];
char SendMsg[]="SendMsgn";
char* wk_p_string;
{
bzero(&addr,sizeof(struct sockaddr_in));
n = recvfrom(sockfd,msg,MAX_MSG_SIZE,0, (struct sockaddr*)&addr,&addrlen);
printf("n = %dn errno = %dn", n, errno);
msg[5] = '';
printf("msg = %sn", msg);
printf("SendMsg Length =%d addrlen = %d SendMsg = %sn", strlen(SendMsg), addrlen, SendMsg);
//wk_p_string = inet_ntoa(addr.sa_data);
//printf(" addr.sin_addr = %s n", wk_p_string);
n = sendto(sockfd, SendMsg, strlen(SendMsg), 0,(struct sockaddr*)&addr,addrlen);
printf("n =%d errno = %dn", n, errno);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in addr;
if(argc!=2)
{
fprintf(stderr,"Usage:%s local_portn",argv[0]);
exit(1);
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if (sockfd
头文件unp.h为 unix环境高级编程里的头文件
#include "unp.h"
#define MAX_MSG_SIZE 10
void udps_respon(int sockfd)
{
struct sockaddr addr;
struct sockaddr_in* p_addr;
int n;
socklen_t addrlen = 0;
char msg[MAX_MSG_SIZE];
char SendMsg[]="SendMsgn";
char* wk_p_string;
{
bzero(&addr,sizeof(struct sockaddr_in));
n = recvfrom(sockfd,msg,MAX_MSG_SIZE,0, (struct sockaddr*)&addr,&addrlen);
printf("n = %dn errno = %dn", n, errno);
msg[5] = '';
printf("msg = %sn", msg);
printf("SendMsg Length =%d addrlen = %d SendMsg = %sn", strlen(SendMsg), addrlen, SendMsg);
//wk_p_string = inet_ntoa(addr.sa_data);
//printf(" addr.sin_addr = %s n", wk_p_string);
n = sendto(sockfd, SendMsg, strlen(SendMsg), 0,(struct sockaddr*)&addr,addrlen);
printf("n =%d errno = %dn", n, errno);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in addr;
if(argc!=2)
{
fprintf(stderr,"Usage:%s local_portn",argv[0]);
exit(1);
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if (sockfd