recv

Linux socket program Demo1(client & server)

瘦欲@ 提交于 2019-12-05 07:21:33
client and server Demo of socket. client send data to server. server send data to client. // this is client #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define BUFF_SIZE 1024 int main(int argc, char* argv[]) { if(argc<=2) { printf("usage:%s ip_address port_number \r\n", basename(argv[0])); return 1; } char* ip = argv[1]; int port = atoi(argv[2]); struct sockaddr_in address; bzero(&address, sizeof(address)); address.sin_family = AF_INET; address.sin

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

落花浮王杯 提交于 2019-12-05 06:16:12
问题 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

recv with non-blocking socket

你说的曾经没有我的故事 提交于 2019-12-05 02:53:17
问题 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>

go中的数据结构通道-channel

谁说胖子不能爱 提交于 2019-12-05 01:49:11
1. channel的使用   很多文章介绍channel的时候都和并发揉在一起,这里我想把它当做一种数据结构来单独介绍它的实现原理。   channel,通道。golang中用于数据传递的一种数据结构。是golang中一种传递数据的方式,也可用作事件通知。 1.1 声明、传值、关闭   使用chan关键字声明一个通道,在使用前必须先创建,操作符 <- 用于指定通道的方向,发送或接收。如果未指定方向,则为双向通道。 1 //声明和创建 2 var ch chan int // 声明一个传递int类型的channel 3 ch := make(chan int) // 使用内置函数make()定义一个channel 4 ch2 := make(chan interface{}) // 创建一个空接口类型的通道, 可以存放任意格式 5 6 type Equip struct{ /* 一些字段 */ } 7 ch2 := make(chan *Equip) // 创建Equip指针类型的通道, 可以存放*Equip 8 9 //传值 10 ch <- value // 将一个数据value写入至channel,这会导致阻塞,直到有其他goroutine从这个channel中读取数据 11 value := <-ch // 从channel中读取数据,如果channel之前没有写入数据

python之tcp自动重连

孤街醉人 提交于 2019-12-04 20:48:18
python之tcp自动重连 操作系统: CentOS 6.9_x64 python语言版本: 2.7.13 问题描述 现有一个tcp客户端程序,需定期从服务器取数据,但由于种种原因(网络不稳定等)需要自动重连。 测试服务器示例代码: https://github.com/mike-zhang/pyExamples/blob/master/socketRelate/tcpServer1_multithread.py 解决方案 ''' tcp client with reconnect E-Mail : Mike_Zhang@live.com ''' #! /usr/bin/env python #-*- coding:utf-8 -*- import os,sys,time import socket def doConnect(host,port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try : sock.connect((host,port)) except : pass return sock def main(): host,port = "127.0.0.1",12345 print host,port sockLocal = doConnect(host,port) while True :

2019-2020-1 20175313 20175328 20175329 实验三 实时系统的移植

Deadly 提交于 2019-12-04 20:13:05
2019-2020-1 20175313 20175328 20175329 实验三 实时系统的移植 实验目的 1.掌握uC/OSII(uCLinux..)的移植过程 2.掌握C,汇编的混合编程 实验仪器 嵌入式实验平台UP-TECH S24101 实验内容、步骤与体会: 实验内容 并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 上方提交代码附件提交测试截图,至少要测试附件中的两个文件 首先先学习一下 命令wc 使用: 利用 man 命令查看 wc 命令使用 实验过程 实验代码 server #include<netinet/in.h> // sockaddr_in #include<sys/types.h> // socket #include<sys/socket.h> // socket #include<stdio.h> // printf #include<stdlib.h> // exit #include<string.h> // bzero #include<unistd.h> #define SERVER_PORT 155316 #define LENGTH_OF_LISTEN_QUEUE 20 #define

recv() data of unknown size with Berkeley Sockets

前提是你 提交于 2019-12-04 19:33:34
I have a code in C++ in which i use recv() from Berkeley Sockets to receive data from a remote host. The issue is that i do not know the size of the data ( which is variable ) so i need some kind of timeout opt ( probably ) to make this work. Since I'm new in sockets programming, i was wondering how does for example a web client handle responses from a server ( eg a server sends the html data to the client ). Does it use some kind of timeout, since it doesn't know how big the page is ? Same with an FTP client. When your data is of variable length, then typically that data is framed within

How to catch a “connection reset by peer” error in C socket?

妖精的绣舞 提交于 2019-12-04 19:28:21
I have a C++ and Qt application which part of it implements a C socket client. Some time ago by app crashed because something happened with the server; the only thing I got from that crash was a message in Qt Creator's Application Output stating recv_from_client: Connection reset by peer I did some research on the web about this "connection reset by peer" error and while some threads here in SO and other places did managed to explain what is going on, none of them tells how to handle it - that is, how can I "catch" the error and continue my application without a crash (particularly the method

SOCKET 通信

淺唱寂寞╮ 提交于 2019-12-04 18:43:08
TCP 服务端 #include<iostream> #include<winsock.h> #pragma comment(lib,"ws2_32.lib") using namespace std; void initialization() { //初始化套接字库 WORD w_req = MAKEWORD(2, 2);//版本号 WSADATA wsadata; /*WSAStartup()程序使用的socket版本、操作系统但会的支持的socket版本,成功时返回0*/ int err = WSAStartup(w_req, &wsadata); if (err != 0) { cout << "初始化套接字库失败!" << endl; } else { cout << "初始化套接字库成功!" << endl; } //检测版本号 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) { cout << "套接字库版本号不符!" << endl; WSACleanup(); } else { cout << "套接字库版本正确!" << endl; } } int main() { //定义长度变量 int send_len = 0, recv_len = 0; int len =

python中的网络通信,socket、select、selectors、socketserver

不羁岁月 提交于 2019-12-04 18:31:23
楔子 网络通信用于获取一个算法在本地运行所需的数据,还可以共享信息实现分布式处理,另外可以用来管理云服务。 python的标准库提供了一些模块来创建网络服务以及访问现有服务ipaddress模块提供了一些类来验证、比较和处理IPV4/IPV6网络地址。底层socket库允许直接访问原生C套接字库,可以用于与任何网络服务通信。selectors提供了一个高层接口,可以同时监视多个套接字,这对于支持网络服务器同时与多个客户通信很有用。select提供了selectors使用的底层API。socketserver中的框架抽象了创建一个新的网络服务器所需要的大量重复性工作。可以结合这些类创建服务器来建立或使用线程以及支持TCP或UDP。应用只需要完成实际的消息处理 ipaddress:Internet地址 介绍 ipaddress模块提供了处理IPV4和IPV6网络地址的类。这些类支持验证,查找网络上的地址和主机,以及其他常见操作 地址 import binascii import ipaddress ''' 最基本的对象表示网络地址本身。可以像ip_address函数传入一个字符串,整数或者字节序列来构造一个地址。 返回值是一个IPv4Address或IPv6Address实例,这取决于使用什么类型的地址 ''' address = "61.135.169.125" addr =