|
我写了一个客户端和服务端程序,客户端用c写的
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 150
int main(void)
{
struct sockaddr_in serv;
char buff[BUFFSIZE];
int sockfd,n;
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
// err_sys("socket error");
printf("error first \n");
bzero((char *) &serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr("192.168.1.14"); /*pt300 */
serv.sin_port = htons(3456); /*time server */
if (sendto(sockfd,buff,BUFFSIZE,0, (struct sockaddr *) &serv, sizeof(serv)) != BUFFSIZE)
printf("error second \n");
printf("can see here \n");
/* recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,&len)*/
if (recvfrom(sockfd, buff, BUFFSIZE, 0, (struct sockaddr *) NULL, (int *) NULL) < 2)
printf("error third \n");
printf("can see here !!!!\n");
buff[n-2] = 0;
printf("-%s-\n",buff);
return 0;
}
========================
服务端是perl写的证明没有问题。
我这个客户端程序,跑到
if (recvfrom(sockfd, buff, BUFFSIZE, 0, (struct sockaddr *) NULL, (int *) NULL) < 2)
就不行了。不知道什么问题。。
谁能解释一下? |
|