待解决问题
在用户态的UDP模式下socket通信,服务器发送数据失败
离问题结束还有0天0小时 |
提问者:songli33
|
提问时间:2012-9-17 09:15
给大家看看我的服务器代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#define TIME_PORT 9090
#define DATA_SIZE 256
int main()
{
int sock_fd;
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen,n;
char buff[DATA_SIZE];
time_t cur_time;
sock_fd = socket(AF_INET,SOCK_DGRAM,0);
if(sock_fd<0){
printf("create socket error\n");
return 0;
}
printf("create socket\n");
/*set ip and port*/
local.sin_family=AF_INET;
local.sin_port=htons(TIME_PORT);
local.sin_addr.s_addr=INADDR_ANY;
/*set ip and port*/
from.sin_family=AF_INET;
from.sin_port=htons(TIME_PORT);
from.sin_addr.s_addr=INADDR_ANY;
fromlen = sizeof(from);
/*bind local to sock*/
if(0!=bind(sock_fd,(struct sockaddr *)&local,sizeof(local))){
printf("bind sock error\n");
close(sock_fd);
return 0;
}
printf("bind socket");
fromlen =sizeof(from);
printf("waiting request from client...\n");
while(1)
{
n = recvfrom(sock_fd,buff,sizeof(buff),0,(struct sockaddr *)&from,&fromlen);
if(n<=0){
printf("RECV DATA ERROR\n");
close(sock_fd);
return 0;
}
buff[n]='\0';
printf("client request: %s\n",buff);
if(0==strncmp(buff,"quit",4)){
break;
}
if(0==strncmp(buff,"time",4)){
int i;
/* cur_time = time(NULL);
strcpy(buff,asctime(gmtime(&cur_time))) ;
* printf("time:");
for(i=0;i<DATA_SIZE||(*(buff+i)=='\0');i++){
printf("%c",*(buff+i));
}
printf("\ntimeend\n");*/
strcpy(buff,"timezml");
printf("timeis: %s\n",buff);
i=sendto(sock_fd,buff,sizeof(buff),0,(struct sockaddr *)&from,fromlen);
if(i<0){
printf("serv send errror\n");
close(sock_fd);
return 0;
}else{
printf("sendbyteis %d\n",i);
}
}
}
close(sock_fd);
return 0;
}
最后sendto发送不了数据,执行结果“serv send errror”,一直找不到发送失败的原因,希望有人能帮忙解答。
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#define TIME_PORT 9090
#define DATA_SIZE 256
int main()
{
int sock_fd;
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen,n;
char buff[DATA_SIZE];
time_t cur_time;
sock_fd = socket(AF_INET,SOCK_DGRAM,0);
if(sock_fd<0){
printf("create socket error\n");
return 0;
}
printf("create socket\n");
/*set ip and port*/
local.sin_family=AF_INET;
local.sin_port=htons(TIME_PORT);
local.sin_addr.s_addr=INADDR_ANY;
/*set ip and port*/
from.sin_family=AF_INET;
from.sin_port=htons(TIME_PORT);
from.sin_addr.s_addr=INADDR_ANY;
fromlen = sizeof(from);
/*bind local to sock*/
if(0!=bind(sock_fd,(struct sockaddr *)&local,sizeof(local))){
printf("bind sock error\n");
close(sock_fd);
return 0;
}
printf("bind socket");
fromlen =sizeof(from);
printf("waiting request from client...\n");
while(1)
{
n = recvfrom(sock_fd,buff,sizeof(buff),0,(struct sockaddr *)&from,&fromlen);
if(n<=0){
printf("RECV DATA ERROR\n");
close(sock_fd);
return 0;
}
buff[n]='\0';
printf("client request: %s\n",buff);
if(0==strncmp(buff,"quit",4)){
break;
}
if(0==strncmp(buff,"time",4)){
int i;
/* cur_time = time(NULL);
strcpy(buff,asctime(gmtime(&cur_time))) ;
* printf("time:");
for(i=0;i<DATA_SIZE||(*(buff+i)=='\0');i++){
printf("%c",*(buff+i));
}
printf("\ntimeend\n");*/
strcpy(buff,"timezml");
printf("timeis: %s\n",buff);
i=sendto(sock_fd,buff,sizeof(buff),0,(struct sockaddr *)&from,fromlen);
if(i<0){
printf("serv send errror\n");
close(sock_fd);
return 0;
}else{
printf("sendbyteis %d\n",i);
}
}
}
close(sock_fd);
return 0;
}
最后sendto发送不了数据,执行结果“serv send errror”,一直找不到发送失败的原因,希望有人能帮忙解答。