#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>

#define MAXLINE 4096

void str_echo(int sockfd) {
  ssize_t n;
  char buff[MAXLINE];

  printf("----echo----\n");
  while((n = read(sockfd, buff, MAXLINE)) > 0) {
    if(write(sockfd, buff, strlen(buff)) != strlen(buff)) {
      printf("write error\n");
      exit(-1);
    }
  }
}

int main() {
  int listenfd, connfd;
  socklen_t clilen;
  pid_t pid;
  struct sockaddr_in servaddr, cliaddr;
  char buff[MAXLINE];

  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if(listenfd < 0) {
    printf("socket error\n");
    exit(-1);
  }

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8000);

  if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    printf("bind error");
    exit(-1);
  }

  if(listen(listenfd, 1024) < 0) {
    printf("listen error");
    exit(-1);
  }
  printf("----server is listening----\n");

  for(;;) {
    clilen = sizeof(cliaddr);

    if((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
      printf("accept error");
      exit(-1);
    }

    printf("ip: %s, port: %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), ntohs(cliaddr.sin_port));
    printf("----------connected-------\n");

    if((pid = fork()) < 0) {
      printf("fork error\n");
      exit(-1);
    }
    else if(pid == 0) { /* child */
      printf("---child pid = %ld process---\n", (long)getpid());
      if(close(listenfd) < 0) {
        printf("---child close listenfd error---\n");
        exit(-1);
      }
      str_echo(connfd);
      if(close(connfd) < 0) {
        printf("---child close connfd error---\n");
        exit(-1);
      }
      exit(0);
    }
    else { /* parent */
      if(close(connfd) < 0) {
        printf("---parent close connfd error---\n");
        exit(-1);
      }
    }
  }
  exit(0);
}

从这些代码中可以体会到客户/服务器网络应用所需的所有基本步骤。

TCP打开文件传输(服务器端并发code)的更多相关文章

  1. TCP打开文件传输(客户端code)

    #include <stdio.h>#include <stdlib.h>#include <arpa/inet.h>#include <sys/types. ...

  2. java实现两台电脑间TCP协议文件传输

    记录下之前所做的客户端向服务端发送文件的小项目,总结下学习到的一些方法与思路. 注:本文参考自<黑马程序员>视频. 首先明确需求,在同一局域网下的机器人A想给喜欢了很久的机器人B发送情书, ...

  3. SSH-Secure-Shell 3.2.9 build283版本,创建直接打开文件传输的快捷方式的方法

    2019-12-31 16:21:23 版本信息: 在安装目录下新建快捷方式 目标填写:"D:\SSH-Secure-Shell\SshClient.exe" /f 图标选择,系统 ...

  4. 通过TCP实现文件传输

    import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.In ...

  5. QT从入门到入土(九)——TCP/IP网络通信(以及文件传输)

    引言 TCP/IP通信(即SOCKET通信)是通过网线将服务器Server端和客户机Client端进行连接,在遵循ISO/OSI模型的四层层级构架的基础上通过TCP/IP协议建立的通讯.控制器可以设置 ...

  6. Linux网络编程:UDP实现可靠的文件传输

    我们知道,用TCP实现文件传输很简单.相对于TCP,因为UDP是面向无连接.不可靠的传输协议,所以我们需要考虑丢包和后发先至(包的顺序)的问题,所以我们想要实现UDP传输文件,则需要解决这两个问题.方 ...

  7. Go网络文件传输

    流程分析 借助TCP完成文件的传输,基本思路如下: 发送方(客户端)向服务端发送文件名,服务端保存该文件名. 接收方(服务端)向客户端返回一个消息ok,确认文件名保存成功. 发送方(客户端)收到消息后 ...

  8. helm-mode打开文件支持中文搜索

    helm-mode打开文件支持中文搜索 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #83949 ...

  9. TCP协议,UDP,以及TCP通信服务器的文件传输

    TCP通信过程 下图是一次TCP通讯的时序图.TCP连接建立断开.包含大家熟知的三次握手和四次握手. 在这个例子中,首先客户端主动发起连接.发送请求,然后服务器端响应请求,然后客户端主动关闭连接.两条 ...

随机推荐

  1. html5——地理位置

    获取地理位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  2. JS——锚点的运用

    锚点的两种形式: 1.<a href="#a">点击到锚点</a> 2.window.location.hash = "#a"; 最后都 ...

  3. SQl基本操作——try catch

    begin try ... end try begin catch ... end catch

  4. servlet-响应信息的content-Type作用

    package servlet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; im ...

  5. Vue项目在IE浏览器报错polyfill-eventsource added missing EventSource to window

    已经安装了babel-polyfill,依然报错.

  6. Ansible 利用playbook批量部署mariadb

    环境说一下 192.168.30.21     ansible 192.168.30.25     client1 192.168.30.26     client2 这里我的ansible环境已经部 ...

  7. Vova and Trophies CodeForces - 1082B(思维题)

    Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trop ...

  8. C#学习笔记_11_方法的隐藏和重写

    11_方法的隐藏和重写 方法的隐藏 需要使用到关键字:new 方法的重写 虚函数: 使用关键字virtual修饰的函数 虚函数可以被子类隐藏,也可以被子类重写 非虚函数只能被子类隐藏 关键字:over ...

  9. BZOJ 1230 Usaco2008 Nov 开关灯

    [题意概述] 给出一个01序列,初始时序列全为0,每次有修改操作或询问操作,修改操作要求把L~R区间中的0变成1,1变成0,查询操作要求输出L~R区间的1的个数 [题解] 线段树. 每次区间修改把区间 ...

  10. Python之路【第一篇】:Python基础1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...