用C语言实现socket文件传输?

 我来答
xude2000
推荐于2016-07-16
知道答主
回答量:6
采纳率:0%
帮助的人:6.1万
展开全部
服务端代码:
#include <stdio.h>
  #include <stdlib.h>
  #include <errno.h>
  #include <string.h>
  #include <sys/types.h>
  #include <netinet/in.h>
  #include <sys/socket.h>
  #include <sys/wait.h>
#define MYPORT 3490 /*定义用户连接端口*/
#define BACKLOG 10 /*多少等待连接控制*/
main()
   {
   int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd
*/
   struct sockaddr_in my_addr; /* my address information */
   struct sockaddr_in their_addr; /* connector's address information */
   int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
   perror("socket");
   exit(1);
   }
my_addr.sin_family = AF_INET; /* host byte order */
   my_addr.sin_port = htons(MYPORT); /* short, network byte order */
   my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
   bzero(&(my_addr.sin_zero),; /* zero the rest of the struct */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr))== -1) {
   perror("bind");
   exit(1);
   }
if (listen(sockfd, BACKLOG) == -1) {
   perror("listen");
   exit(1);
   }
while(1) { /* main accept() loop */
   sin_size = sizeof(struct sockaddr_in);
   if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \
   &sin_size)) == -1) {
   perror("accept");
   continue;
   }
   printf("server: got connection from %s\n", \
   inet_ntoa(their_addr.sin_addr));
   if (!fork()) { /* this is the child process */
   if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
   perror("send");
   close(new_fd);
   exit(0);
   }
   close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
   }
   }

客户代码:
#include <stdio.h>
  #include <stdlib.h>
  #include <errno.h>
  #include <string.h>
  #include <sys/types.h>
  #include <netinet/in.h>
  #include <sys/socket.h>
  #include <sys/wait.h>
#define PORT 3490 /* 客户机连接远程主机的端口 */
#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */
int main(int argc, char *argv[])
   {
   int sockfd, numbytes;
   char buf[MAXDATASIZE];
   struct hostent *he;
   struct sockaddr_in their_addr; /* connector's address information */
if (argc != 2) {
   fprintf(stderr,"usage: client hostname\n");
   exit(1);
   }
if ((he=gethostbyname(argv[1])) == NULL) { /* get the host info */
   herror("gethostbyname");
   exit(1);
   }
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
   perror("socket");
   exit(1);
   }
their_addr.sin_family = AF_INET; /* host byte order */
  their_addr.sin_port = htons(PORT); /* short, network byte order */
  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  bzero(&(their_addr.sin_zero),; /* zero the rest of the struct */
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct
sockaddr)) == -1) {
   perror("connect");
   exit(1);
   }
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
   perror("recv");
   exit(1);
   }
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
   }
百度网友403775daf
2011-05-11 · TA获得超过182个赞
知道小有建树答主
回答量:274
采纳率:0%
帮助的人:169万
展开全部
socket + FILE 灰常简单~!
SOCKET 例子多如牛毛,FILE文件操作更是随处可见~!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式