69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdlib.h>
main(argc,argv)
int argc;
char **argv;
{
int sockfd;
FILE *fp1;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
char msg[1];
sockfd=socket(AF_INET,SOCK_STREAM,0);
fp1=fopen("file.txt","r");
if (sockfd<0)
printf("Opening stream socket");
if ((hp=gethostbyname(argv[1]))==NULL){
fprintf(stderr,"%s:unknown host\n",argv[1]);
exit(2);
}
server.sin_family=AF_INET;
bcopy((char *)hp->h_addr,(char *)&server.sin_addr.s_addr,hp->h_length);
server.sin_port=htons(atoi(argv[2]));
if (connect(sockfd,(struct sockaddr *)&server,sizeof(server))<0)
printf("connecting stream socket");
while((fread(msg,1,1,fp1))>0){
//printf("Enter send message: %s",msg);
//scanf("%s",msg);
if(!strlen(msg)) break;
if(send(sockfd,msg,strlen(msg),0)<0)
printf("sending message");
bzero(msg,sizeof(msg));
}
printf("EOF...disconnect\n");
close(sockfd);
exit(0);
}
[root@localhost ~]# gdb ./a.out
GNU gdb (GDB) CentOS (7.0.1-45.el5.centos)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/a.out...(no debugging symbols found)...done.
(gdb) bt
No stack.