recv

Socket无连接简单实例

余生颓废 提交于 2019-12-16 22:34:26
使用无连接的套接字,我们能够在自我包含的数据包里发送消息,采用独立的读函数读取消息,读取的消息是使用独立的发送函数发送的。但是UDP数据包不能保证可靠传输,存在许多的因素,比如网络繁忙等等,都有可能阻止数据包到达指定的目的地。 (1) UDP的简单应用: 由于UDP是一种无连接的协议。因此,为了使服务器应用能够发送和接收UDP数据包,则需要做两件事情: 创建一个Socket对象; 将创建的套接字对象与本地IPEndPoint进行绑定。 完成上述步骤后,那么创建的套接字就能够在IPEndPoint上接收流入的UDP数据包,或者将流出的UDP数据包发送到网络中任意其他设备商。使用UDP进行通信时,不需要TCP连接。因为异地的主机之间没有建立连接,所以UDP不能使用标准的Send()和Receive()t套接字方法,而是使用两个其他的方法:SendTo()和ReceiveFrom()。 SendTo()方法指定要发送的数据,和目标机器的IPEndPoint。该方法有多种不同的使用方法,可以根据具体的应用进行选择,但是至少要指定数据包和目标机器。如下: SendTo(byte[] data,EndPoint Remote) ReceiveFrom()方法同SendTo()方法类似,但是使用EndPoint对象声明的方式不一样。利用ref修饰,传递的不是一个EndPoint对象

网络编程

穿精又带淫゛_ 提交于 2019-12-16 11:21:10
1 .简述socket 通信原理 如上图,socket通信建立在应用层与TCP/IP协议组通信(运输层)的中间软件抽象层,它是一组接口,在设计模式中,socket其实就是一个门面模式,它把复杂的TCP/IP协议组隐藏在Socket接口后面,对于用户来说,一组简单的接口就是全部,让socket去组织数据,以符合指定的协议。 所以,经常对用户来讲, socket就是ip+prot 即IP地址(识别互联网中主机的位置)+port是程序开启的端口号  socket通信如下: 客户端 # _*_ coding: utf-8 _*_ import socket ip_port = ('127.0.0.1',9696) link = socket.socket(socket.AF_INET,socket.SOCK_STREAM) link.connect(ip_port) print("开始发送数据") cmd = input("client请输入要发送的数据>>>>").strip() link.send(cmd.encode('utf-8')) recv_data = link.recv(1024) print("这是受到的消息:",recv_data) link.close() 服务端 # _*_ coding: utf-8 _*_ import socket ip_port = (

sys模块 json pickle模块

不羁岁月 提交于 2019-12-14 19:45:15
# sys模块# import sys# sys.path# sys.argv# sys.exit() # 脚本退出# print('[%s]'%('#'*1))# print('[%s]'%('#'*2))# print('[%s]'%('#'*3))# print('[%s]'%('#'*4))# print('[%s]'%('#'*5))# print('[%-50s]'%('#'*1))# print('[%-50s]'%('#'*2))# print('[%-50s]'%('#'*3))# print('[%-50s]'%('#'*4))# print('[%-50s]'%('#'*5))'''[#][##][###][####][#####][# ][## ][### ][#### ][##### ]'''# print('%d%%'%30) # 30%# print('[%%-%ds]'%50)# print(('[%%-%ds]'%50)%('#'*1))# print(('[%%-%ds]'%50)%('#'*2))# print(('[%%-%ds]'%50)%('#'*3))# print(('[%%-%ds]'%50)%('#'*4))# print(('[%%-%ds]'%50)%('#'*5))'''[%-50s][# ][## ][### ][####

Recv returning zero incorrectly [closed]

穿精又带淫゛_ 提交于 2019-12-14 03:37:32
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I have an interesting problem, I send bytes of data to my server, from my client, and the recv() function always returns zero. I have verified that the function is indeed getting the data and placing it into the char array correctly. It just returns zero regardless of the number of bytes received. I have

recv() returns always 0

六月ゝ 毕业季﹏ 提交于 2019-12-14 02:14:16
问题 I've a problem with recv() function that I can't explain: it always returns 0. I've a client/server application in which the server simply has to receive a string from the client through the internet (different pc). There are no connectivity problems, and I also tried to send a string from server to client: it worked. I search in the blog and what I found, recv() socket function returning data with length as 0, non-blocking recv returns 0 when disconnected, Recv returning zero incorrectly,

C++ Winsock recv() buffer junk

时光怂恿深爱的人放手 提交于 2019-12-13 04:55:55
问题 I'm writing a console appication in cpp that sends control commands from a file via TCP to a host machine and receives a response. All those informations are shown on screen and logged to a file and this is the actual problem. The output string seems to store junk for any reason, even if I try to set a fixed length. EDIT: cleaned up the code and took care of the return value of recv(). The only thing I don't get yet is that the 2nd recv line in my logfile is filled with junk. Maybe one of you

Raw Socket Linux send/receive a packet

匆匆过客 提交于 2019-12-12 07:12:43
问题 Have some problems in receiving packets. I can receive and read incoming packets, but I think i do not get a handshake with any host. I only want to send a packet to a remote computer with an open port on receiving an answer to see the TTL(time to live) and the window size. Does anyone have an idea where the errors are? (I don't have very deep knowledge in C programming) CODE: #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <errno.h> #include

Blocking recvfrom with select system call

这一生的挚爱 提交于 2019-12-12 04:54:08
问题 I have a UDP client which has to receive form two different sockets. I am using select system call to multiplex the recv call. But I am seeing that the client is blocked inside the second recv call. How can I resolve this issue? struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; int activity; FD_ZERO(&socketfds); FD_SET(usocket,&socketfds); max_sd = std::max(max_sd, usocket); FD_SET(msocket,&socketfds); max_sd = std::max(max_sd, msocket); rset = socketfds; do { rset = socketfds;

Detour hook send/recv winsock

孤街浪徒 提交于 2019-12-12 03:54:31
问题 Im trying to hook the send/recv functions from Ultima Online client usinf MS Detour. I've found a c++ dll/injector source out there, but it is not working. The dll is injected but the functions is not being hooked. When the injector start the client, the dll throw 3 box saying that it was injected and hooked both recv/send, but nothing happens when the client start the comminication injector.cpp #include <windows.h> #include <detours.h> #include <cstdio> #pragma comment(lib,"detours.lib") int

Receive recv data until end of stream (using HTTP)?

久未见 提交于 2019-12-12 02:24:46
问题 I'm trying out C++ sockets for the first time, and I've hit my first obstacle. I've send some data to google using the send function ( GET / HTTP/1.1\r\n\r\n ), and now I'm trying to receive the response. My current code: char buffer[256]; std::string result = ""; int resultSize = 0; bool receive = true; while (receive) { resultSize = recv(dataSocket, buffer, sizeof(buffer) - 1, 0); buffer[resultSize] = '\0'; // Add NULL terminating character to complete string result += buffer; for (int i =