先贴程序:
/******* 客户端程序 client.c ************/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct sockaddr_in new_fd;
struct hostent *host;
int portnumber,nbytes;
char buff[1024];
if(argc!=3)
{
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Get host name error\n");
exit(1);
}
if((portnumber=atoi(argv[2]))<0)
{
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket Creat Error:%s\a\n",strerror(errno));
exit(1);
}
else
printf("Socket Creat .......[ok]\nThe Descriptor is %d\n",sockfd);
printf("1\n");
bzero(&server_addr,sizeof(server_addr));
printf("2\n");
server_addr.sin_family=AF_INET;
printf("3\n");
server_addr.sin_port=htons(portnumber);
printf("4\n");
server_addr.sin_addr =*((struct in_addr *)host->h_addr);
/*server_addr.sin_addr=*((struct in_addr *)host->h_addr); */
printf("ready to connet.....");
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
else
printf("Connect to Host.....[ok]\n");
while(1)
{
bzero(buffer,sizeof(buffer));
printf("I am ready to read STDIN_FILENO\n");
if((nbytes=read(STDIN_FILENO,buff,1024))==-1)
{
fprintf(stderr,"Send Error:%s\n",strerror(errno));
exit(1);
}
printf("I have read STDIN_FILENO\n");
sleep(3);
buff[nbytes]='\n';
buff[nbytes+1]='\0';
printf("I am ready to write buff to sockfd\n");
sleep(1);
if((write(sockfd,buff,strlen(buff)))==-1)
{
perror("send");
}
sleep(3);
printf("I have written buff to sockfd\n");
sleep(3);
printf("I am ready to read sockfd\n");
bzero(buffer,sizeof(buffer));
if((nbytes=read(sockfd,buffer,1024))==-1)
{
fprintf(stderr,"Send Error:%s\n",strerror(errno));
exit(1);
}
buffer[nbytes]='\n';
printf("I have received:%s\n",buffer);
sleep(1);
}
/* 结束通讯 */
/*close(sockfd);
exit(0); */
}
也是很简单的一个程序,我在虚拟机与一个TCP调试工具进行连接已经成功,可在9260板子上跑,就停在了打印的4处,等一会就会出现time out 然后打印 ready to connect。。。。然后就没有动静了,这是怎么回事啊。。。。。。。 |