recv

The behavior of send() and recv() in socket communication

寵の児 提交于 2019-12-04 14:43:45
The following is the setup: Server Client | | accept connect | | v | send msg1-> | | | v v recv <- send | | v v send msg2-> recv | | v v close Here is my question: 1. Client actually receives msg1 before it closes, why is it like this? 2. send msg2 returns normally. Since client closes after receiving msg1, why is send msg2 successful? P.S. I'm using stream socket for TCP. The recv function will get whatever is next in the receive buffer. In the case of the client, if the socket is a datagram socket, what is next is msg1 . If it is a stream socket then message boundaries are not maintained so

UDP recv/recvfrom multiple senders

梦想与她 提交于 2019-12-04 13:40:32
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 question is whether or not I can assume that a buffer that I get from a single return from recvfrom is from a

Send/Receive messages at the same time socket python

╄→尐↘猪︶ㄣ 提交于 2019-12-04 12:34: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((host,port)) name = input(str("Please enter your username: ")) print("") print("Server is waiting for

PHP websocket之聊天室实现

萝らか妹 提交于 2019-12-04 12:11:40
PHP部分 <?php error_reporting(E_ALL); set_time_limit(0);// 设置超时时间为无限,防止超时 date_default_timezone_set('Asia/shanghai'); class WebSocket { const LOG_PATH = '/tmp/'; const LISTEN_SOCKET_NUM = 9; /** * @var array $sockets * [ * (int)$socket => [ * info * ] * ] * todo 解释socket与file号对应 */ private $sockets = []; private $master; public function __construct($host, $port) { try { $this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 设置IP和端口重用,在重启服务器后能重新使用此端口; socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1); // 将IP和端口绑定在服务器socket上; socket_bind($this->master, $host, $port); //

Ending “recv()” loop when all information is Read using Winsock

寵の児 提交于 2019-12-04 05:18:37
I am having an issue in my recv() loop for winsock. I am trying to terminate the loop when iResult==0, however, the loop only ends when the socket closes. It appears to be hanging at the very last recv() where iResult would equal 0. So any ideas on how to terminate the loop effectively? My ultimate goal (whether iResult == 0 or not; perhaps I am going about this the wrong way) is to stop the loop when all the sent information has been read. Here is the loop. do { iResult = recv(socket, recvbuf, BUFLEN-1, 0); if(iResult > 0){ // Null byte :) // Remove that garbage >:( recvbuf[iResult] = '\0';

recv with non-blocking socket

一世执手 提交于 2019-12-03 21:23:45
I am trying to implement non-blocking for socket recv and the problem is that I got an error -1 when there in no data but I expect to get EAGAIN error. Socket is set definitely to non-blocking state, I checked flags = fcntl(s, F_GETFL, 0) for O_NONBLOCK flag. Thanks a lot in advance! #include <arpa/inet.h> #include <linux/if_packet.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h> #include <netinet/ether.h> #include <unistd.h> #include <fcntl.h> #include <asm-generic/errno-base.h> #include <assert.h> #define ETH

什么是PHP Socket?

三世轮回 提交于 2019-12-03 20:59:14
什么是 Socket? Socket 的中文翻译过来就是“套接字”。套接字是什么,我们先来看看它的英文含义:插座。 Socket 就像一个电话插座,负责连通两端的电话,进行点对点通信,让电话可以进行通信,端口就像插座上的孔,端口不能同时被其他进程占用。而我们建立连接就像把插头插在这个插座上,创建一个 Socket 实例开始监听后,这个电话插座就时刻监听着消息的传入,谁拨通我这个“IP 地址和端口”,我就接通谁。 实际上,Socket 是在应用层和传输层之间的一个抽象层,它把 TCP/IP 层复杂的操作抽象为几个简单的接口,供应用层调用实现进程在网络中的通信。Socket 起源于 UNIX,在 UNIX 一切皆文件的思想下,进程间通信就被冠名为 文件描述符(file descriptor) ,Socket 是一种“打开—读/写—关闭”模式的实现,服务器和客户端各自维护一个“文件”,在建立连接打开后,可以向文件写入内容供对方读取或者读取对方内容,通讯结束时关闭文件。 另外我们经常说到的 Socket 所在位置 如下图: Socket 通信过程 Socket 保证了不同计算机之间的通信,也就是网络通信。对于网站,通信模型是服务器与客户端之间的通信。两端都建立了一个 Socket 对象,然后通过 Socket 对象对数据进行传输。通常服务器处于一个无限循环,等待客户端的连接。 一图胜千言

epoll_wait() receives socket closed twice (read()/recv() returns 0)

喜你入骨 提交于 2019-12-03 20:19:48
We have an application that uses epoll to listen and process http-connections. Sometimes epoll_wait() receives close event on fd twice in a "row". Meaning: epoll_wait() returns connection fd on which read()/recv() returns 0. This is a problem, since I have malloc:ed pointer saved in epoll_event struct (struct epoll_event.data.ptr) and which is freed when fd(socket) is detected as closed the first time. Second time it crashes. This problem occurs very rarely in real use (except one site, which actually has around 500-1000 users per server). I can replicate the problem using http siege with

Get the number of bytes available in socket by 'recv' with 'MSG_PEEK' in C++

醉酒当歌 提交于 2019-12-03 18:51:20
问题 C++ has the following function to receive bytes from socket, it can check for number of bytes available with the MSG_PEEK flag. With MSG_PEEK , the returned value of 'recv' is the number of bytes available in socket: #include <sys/socket.h> ssize_t recv(int socket, void *buffer, size_t length, int flags); I need to get the number of bytes available in the socket without creating buffer (without allocating memory for buffer ). Is it possible and how? 回答1: You're looking for is ioctl(fd

Determine how many bytes can be sent with winsock (FIONWRITE)?

馋奶兔 提交于 2019-12-03 16:49:35
With select I can determine if any bytes can be received or sent without blocking. With this function I can determine how many bytes can be received: function BytesAvailable(S: TSocket): Integer; begin if ioctlsocket(S, FIONREAD, Result) = SOCKET_ERROR then Result := -1; end; Is there also a way to determine how many bytes can be sent? So I can be sure when I call send with N bytes, it will return exactly N bytes sent (or SOCKET_ERROR) but not less (send buffer is full). FIONWRITE is not available for Winsock. According to MVP Alexander Nickolov , there is no such facility in Windows. He also