主题:[讨论]急,UDP数据报传输错误
我的目的是想在客户端发送已知的客户的用户名和密码,然后在服务器端打印这些数据,但是运行后,服务器端竟然没有输出我想要的数据,在password那一行竟然出现了乱码,我想是不是对接收到的结构体的使用有问题.谢谢
这是客户端程序:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SERVER_PORT 8017
#define MSG_BUF_SIZE 512
int port=SERVER_PORT ;
struct client
{
char *name ;
char *password ;
};
int main()
{
int sockfd ;
int count=0 ;
int flag ;
char buf[MSG_BUF_SIZE] ;
struct sockaddr_in address ;
struct client client ;
// create UDP socket
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
fprintf(stderr,"socket error") ;
exit(1) ;
}
memset(&address,0,sizeof(address)) ;
address.sin_family=AF_INET ;
address.sin_addr.s_addr=inet_addr("127.0.0.1") ;
address.sin_port=htons(port) ;
flag=1 ;
while(flag)
{
sprintf(buf,"Packet %d\n",count) ;
/*
if(count>30)
{
sprintf(buf,"overn") ;
flag=0 ;
}
bzero(buf, MSG_BUF_SIZE+1) ;
strcpy(buf,"welcome to linux world!\n") ;
sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&address,sizeof(address));
count++ ;
*/
// client.name="liu xiaofeng" ;
// client.password="asdfghjkl" ;
// bzero(client.name,101) ;
// bzero(client.password,101) ;
// strcpy(client.name,"liu xiaofeng") ;
// strcpy(client.password,"liu xiaofeng") ;
client.name="liu xiaofeng" ;
client.password="qwertyui" ;
sendto(sockfd,&client,sizeof(client),0,(struct sockaddr *)&address,sizeof(address)) ;
}
return 0 ;
}
这是服务器端程序:
#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<netdb.h>
#include<stdlib.h>
#define SERVER_PORT 8017
#define MSG_BUF_SIZE 512
//#define LOCAL_HOST "localhost.localdomain"
int port=SERVER_PORT ;
char *hostname="127.0.0.1" ;
struct client
{
char *name ;
char *password ;
};
int main()
{
int sinlen ;
int port=SERVER_PORT ;
char message[MSG_BUF_SIZE] ;
int sockfd ;
struct sockaddr_in sin ;
struct client client1 ;
struct hostent *server_host_name ;
if((sockfd=socket(PF_INET,SOCK_DGRAM,0))==-1)
{
fprintf(stderr,"socket error") ;
exit(1) ;
}
//get host name
server_host_name=gethostbyname("127.0.0.1") ;
bzero(&sin,sizeof(sin)) ;
sin.sin_family=AF_INET ;
sin.sin_addr.s_addr=htonl(INADDR_ANY) ;
sin.sin_port=htons(port) ;
if((bind(sockfd,(struct sockaddr *)&sin,sizeof(sin)))==-1)
{
fprintf(stderr,"bind error") ;
exit(1) ;
}
// while(1)
// {
sinlen=sizeof(sin) ;
recvfrom(sockfd,&client1,sizeof(struct client),0,(struct sockaddr *)&sin,&sinlen) ;
/*
printf("nData come from server:n%s\n",message) ;
if(strncmp(message,"over",4)==0) break ;
*/
printf("client's name is :n%s\n",client1.name) ;
printf("client's password is :n%s\n",client1.password) ;
// }
close(sockfd) ;
return 0 ;
}
这是客户端程序:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SERVER_PORT 8017
#define MSG_BUF_SIZE 512
int port=SERVER_PORT ;
struct client
{
char *name ;
char *password ;
};
int main()
{
int sockfd ;
int count=0 ;
int flag ;
char buf[MSG_BUF_SIZE] ;
struct sockaddr_in address ;
struct client client ;
// create UDP socket
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
fprintf(stderr,"socket error") ;
exit(1) ;
}
memset(&address,0,sizeof(address)) ;
address.sin_family=AF_INET ;
address.sin_addr.s_addr=inet_addr("127.0.0.1") ;
address.sin_port=htons(port) ;
flag=1 ;
while(flag)
{
sprintf(buf,"Packet %d\n",count) ;
/*
if(count>30)
{
sprintf(buf,"overn") ;
flag=0 ;
}
bzero(buf, MSG_BUF_SIZE+1) ;
strcpy(buf,"welcome to linux world!\n") ;
sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&address,sizeof(address));
count++ ;
*/
// client.name="liu xiaofeng" ;
// client.password="asdfghjkl" ;
// bzero(client.name,101) ;
// bzero(client.password,101) ;
// strcpy(client.name,"liu xiaofeng") ;
// strcpy(client.password,"liu xiaofeng") ;
client.name="liu xiaofeng" ;
client.password="qwertyui" ;
sendto(sockfd,&client,sizeof(client),0,(struct sockaddr *)&address,sizeof(address)) ;
}
return 0 ;
}
这是服务器端程序:
#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<netdb.h>
#include<stdlib.h>
#define SERVER_PORT 8017
#define MSG_BUF_SIZE 512
//#define LOCAL_HOST "localhost.localdomain"
int port=SERVER_PORT ;
char *hostname="127.0.0.1" ;
struct client
{
char *name ;
char *password ;
};
int main()
{
int sinlen ;
int port=SERVER_PORT ;
char message[MSG_BUF_SIZE] ;
int sockfd ;
struct sockaddr_in sin ;
struct client client1 ;
struct hostent *server_host_name ;
if((sockfd=socket(PF_INET,SOCK_DGRAM,0))==-1)
{
fprintf(stderr,"socket error") ;
exit(1) ;
}
//get host name
server_host_name=gethostbyname("127.0.0.1") ;
bzero(&sin,sizeof(sin)) ;
sin.sin_family=AF_INET ;
sin.sin_addr.s_addr=htonl(INADDR_ANY) ;
sin.sin_port=htons(port) ;
if((bind(sockfd,(struct sockaddr *)&sin,sizeof(sin)))==-1)
{
fprintf(stderr,"bind error") ;
exit(1) ;
}
// while(1)
// {
sinlen=sizeof(sin) ;
recvfrom(sockfd,&client1,sizeof(struct client),0,(struct sockaddr *)&sin,&sinlen) ;
/*
printf("nData come from server:n%s\n",message) ;
if(strncmp(message,"over",4)==0) break ;
*/
printf("client's name is :n%s\n",client1.name) ;
printf("client's password is :n%s\n",client1.password) ;
// }
close(sockfd) ;
return 0 ;
}