recv

读书笔记_python网络编程3_(3)

北城以北 提交于 2019-12-06 13:12:47
3.TCP:传输控制协议 第一个版本在1974年定义,建立在网际层协议(IP)提供的数据包传输技术之上。TCP使程序可以使用连续的数据流进行相互通信。 除非网络原因导致连接中断/冻结,TCP都能保证将数据流完好无缺地传输至接收方,不会发生丢包、重包、乱序问题。 传输文档/文件的协议都使用TCP,包括浏览网页、文件传输、电子邮件的所有主要机制,也是人机间进行长对话的协议的基础之一,如SSH/聊天协议 经过30年的改进,现代TCP相当精良,除了协议设计专家,很少有人能再改进现代TCP栈的性能,就算是消息队列这种对性能要求很高的程序, 也会选择TCP作为传输媒介。 3.1. TCP工作原理:数据包被隐藏在协议层之下,程序只需向目标机器发送流数据,TCP会将丢失的信息重传,保证信息成功到达目标机器 经典定义来自1981年的RFC 793,如何提供可靠连接,基本原理: 3.1.1. 每个TCP数据包都有一个序列号,接收方通过该序列号将响应数据包正确排序,也可通过该序列号发现传输序列中丢失的数据包,并请求进行重传。 3.1.2. TCP不使用顺序整数作为数据包的序列号,而是通过计数器来记录发送的字节数。如,包含1024字节的数据表的序列号为7200, 下一个数据包序列号就为8224。网络栈无需记录如何将数据流分割为数据包,需要重传,可以使用另一种分割方式,将数据流分为多个新数据包(需要传输更多

读书笔记_python网络编程3_(3)

眉间皱痕 提交于 2019-12-06 13:12:25
3.TCP:传输控制协议 第一个版本在1974年定义,建立在网际层协议(IP)提供的数据包传输技术之上。TCP使程序可以使用连续的数据流进行相互通信。 除非网络原因导致连接中断/冻结,TCP都能保证将数据流完好无缺地传输至接收方,不会发生丢包、重包、乱序问题。 传输文档/文件的协议都使用TCP,包括浏览网页、文件传输、电子邮件的所有主要机制,也是人机间进行长对话的协议的基础之一,如SSH/聊天协议 经过30年的改进,现代TCP相当精良,除了协议设计专家,很少有人能再改进现代TCP栈的性能,就算是消息队列这种对性能要求很高的程序, 也会选择TCP作为传输媒介。 3.1. TCP工作原理:数据包被隐藏在协议层之下,程序只需向目标机器发送流数据,TCP会将丢失的信息重传,保证信息成功到达目标机器 经典定义来自1981年的RFC 793,如何提供可靠连接,基本原理: 3.1.1. 每个TCP数据包都有一个序列号,接收方通过该序列号将响应数据包正确排序,也可通过该序列号发现传输序列中丢失的数据包,并请求进行重传。 3.1.2. TCP不使用顺序整数作为数据包的序列号,而是通过计数器来记录发送的字节数。如,包含1024字节的数据表的序列号为7200, 下一个数据包序列号就为8224。网络栈无需记录如何将数据流分割为数据包,需要重传,可以使用另一种分割方式,将数据流分为多个新数据包(需要传输更多

Sending data from a struct, socket programming

前提是你 提交于 2019-12-06 13:09:03
问题 I have an assignment for school, and one part is to send a set of ints, chars char* from a client to a server using socket programming. Sending ints or chars works just fine, but is there any way to send the entire struct as one packet? Read about serializing, but I cant seem to make it work. Here's a code snippet: The struct looks like this: struct Msg { int a; char b; char *user; }; Client: init variables and such... int netcom(char* ip, int port) { sd = socket(PF_INET, SOCK_STREAM, IPPROTO

C Windows buffer size

五迷三道 提交于 2019-12-06 12:34:00
In windows lets say i'm using the recv function to receive data from a socket. I'm curious how big would an optimal buffer be? I could make it 1024 bytes or I could make it 51200 bytes, or bigger. I'm wondering which one would be better for performance. This doesn't only apply to the recv function, lets say im reading a large text file, do i want a very large buffer, or a smaller buffer? THe operating system performs its own buffering, so the size of your buffer does not really matter. the performance penalty lies in the function call: a 1 byte buffer will be inefficient because it will

UDP recv/recvfrom multiple senders

╄→гoц情女王★ 提交于 2019-12-06 07:40:59
问题 Good Day, I'm developing an application in VC++ that communicates using UDP protocol with winsock on Windows XP. Previously I've been able to assume that all packets being received by the tool were from a single target. However I'm now doing a broadcast receive. The listening thread has minimal overhead and should spend all of its time on the below line: rv = recvfrom(socket, p_buffer_p, p_size, 0, (sockaddr*)&clientService_in, //This is set to do a broadcast recv &SenderAddrSize); My

Send/Receive messages at the same time socket python

邮差的信 提交于 2019-12-06 07:33:19
问题 I have been working on a simple python socket chat room where the client and server can send messages to each other. The issue that I came across was that the server and client can only send one message at a time. I want it to work like any other chat room, where I could receive a message when I am sending a message, any help will help greatly Server.py import socket import sys s = socket.socket() host = socket.gethostname() print(" server will start on host : ", host) port = 8080 s.bind(

Python实现简易HTTP服务器与MINI WEB框架(利用WSGI实现服务器与框架解耦)

夙愿已清 提交于 2019-12-06 06:44:39
本文描述如果简单实现自定义Web服务器与自定义简易框架,并且不断进行版本迭代,从而清晰的展现服务器与Web框架之间是如何结合、如何配合工作的。以及WSGI是什么。 一、选取一个自定义的服务器版本 参照 https://www.cnblogs.com/leokale-zz/p/11957768.html 中的各种服务器实现版本,我们选择比较简单的 多进程版本 作为演示版本 。 代码如下: import socket import re import multiprocessing def handle_request(new_socket): # 接收请求 recv_msg = "" recv_msg = new_socket.recv(1024).decode("utf-8") if recv_msg == "": print("recv null") new_socket.close() return # 从请求中解析出URI recv_lines = recv_msg.splitlines() print(recv_lines.__len__()) # 使用正则表达式提取出URI ret = re.match(r"[^/]+(/[^ ]*)", recv_lines[0]) if ret: # 获取URI字符串 file_name = ret.group(1) # 如果URI是

一步一步创建聊天程序2-利用epoll来创建简单的聊天室

放肆的年华 提交于 2019-12-06 06:27:01
如图,这个是看视频时,最后的作业,除了客户端未使用select实现外,其它的要求都有简单实现。 服务端代码如下: #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <sys/epoll.h> #include <netinet/in.h> #include <unistd.h> #include<netinet/tcp.h> #define MAX_LISTEN 10 #define EPOLL_SIZE 100 struct message { int target_id; char buf[100]; }; struct user_password { char username[100]; char password[100]; }; //设置保活参数 keepalive_time保活时间,keepalive_intvl保活间隔,keepalive_probes保活探测数。 int set_keepalive(int sockfd,int keepalive_time, int keepalive_intvl, int keepalive_probes) { int optval; socklen_t

Whats the addrlen field in recvfrom() used for?

独自空忆成欢 提交于 2019-12-06 06:04:17
I'm using recvfrom in my program to get DGRAM data from a server I specify in src_addr. However, I'm not sure why I need to initialize and pass in addrlen. I read the man page and I didn't really understand what it's getting at. If src_addr is not NULL, and the underlying protocol provides the source address, this source address is filled in. When src_addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL. The argument addrlen is a value-result argument, which the caller should initialize before the call to the size of the buffer associated with src_addr

python实现一个简单的网络聊天程序

那年仲夏 提交于 2019-12-06 05:47:21
一、Linux Socket 1.Linux Socke基本上就是BSD Socket(伯克利套接字) 伯克利套接字的应用编程接口(API)是采用C语言的进程间通信的库,经常用在计算机网络间的通信。BSD Socket的应用编程接口已经是网络套接字的抽象标准。大多数其他程序语言使用一种相似的编程接口。由于伯克利套接字是第一个socket,大多数程序员很熟悉它们,所以大量系统把伯克利套接字作为其主要的网络API。 主要的头文件如下,不同的系统可能具体不同。 <sys/socket.h> BSD socket 核心函数和数据结构。 <netinet/in.h> AF_INET 和AF_INET6 地址家族和他们对应的协议家族PF_INET 和PF_INET6。在互联网编程中广泛使用,包括IP地址以及TCP和UDP端口号。 <sys/un.h> PF_UNIX/PF_LOCAL 地址家族。用于运行在一台计算机上的程序间的本地通信,不用在网络中。 <arpa/inet.h> 和IP地址相关的一些函数。 <netdb.h> 把协议名和主机名转化成数字的一些函数。 2.API函数 这些是伯克利套接字提供的库函数: (1)socket() 创造某种类型的套接字,分配一些系统资源,用返回的整数识别。 (2)bind() 一般是用在服务器这边,和一个套接字地址结构相连