当前位置: 代码迷 >> C语言 >> [求助]tcp/ip 程序问题。。大家来探讨一下
  详细解决方案

[求助]tcp/ip 程序问题。。大家来探讨一下

热度:449   发布时间:2006-07-31 23:33:53.0
[求助]tcp/ip 程序问题。。大家来探讨一下
我写了一个客户端和服务端程序,客户端用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)
就不行了。不知道什么问题。。
谁能解释一下?
搜索更多相关的解决方案: tcp  include  sys  socket  

----------------解决方案--------------------------------------------------------
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)

===========

wo ju de ying gai yong:

if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)

bu yi ding dui a


----------------解决方案--------------------------------------------------------
  相关解决方案