本文整理汇总了C++中socket_recv函数的典型用法代码示例。如果您正苦于以下问题:C++ socket_recv函数的具体用法?C++ socket_recv怎么用?C++ socket_recv使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_recv函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: sys_channel_read
int
sys_channel_read( SysChannel channel, void* buffer, int size )
{
int len = size;
char* buf = (char*) buffer;
while (len > 0) {
int ret = socket_recv(channel->fd, buf, len);
if (ret < 0) {
if (errno == EINTR)
continue;
if (errno == EWOULDBLOCK || errno == EAGAIN)
break;
D( "%s: after reading %d bytes, recv() returned error %d: %s\n",
__FUNCTION__, size - len, errno, errno_str);
return -1;
} else if (ret == 0) {
break;
} else {
buf += ret;
len -= ret;
}
}
return size - len;
}
开发者ID:hongjiujing,项目名称:Opensource,代码行数:25,代码来源:sysdeps_qemu.c
示例2: test_tcp
int test_tcp() {
int fd = socket_new(SOCK_STREAM);
fd =socket_bind(fd, "", 9000);
fd = socket_listen(fd);
printf("socket fd: %d\n", fd);
char cip[16] = {0};
int connfd = socket_accept(fd, cip);
if (connfd < 0) {
printf("accept failed\n");
return -1;
}
printf("start to recv\n");
char* msg = (char*)socket_recv(connfd);
printf("body=%s\n", msg);
socket_send(connfd, "back from server", strlen("back from server"));
free(msg);
close(connfd);
close(fd);
return 0;
}
开发者ID:billypu,项目名称:lua-lab,代码行数:27,代码来源:test_server.c
示例3: asyncReader_read
AsyncStatus
asyncReader_read(AsyncReader* ar)
{
int ret;
if (ar->pos >= ar->buffsize) {
return ASYNC_COMPLETE;
}
do {
ret = socket_recv(ar->io->fd, ar->buffer + ar->pos, ar->buffsize - ar->pos);
if (ret == 0) {
/* disconnection ! */
errno = ECONNRESET;
return ASYNC_ERROR;
}
if (ret < 0) {
if (errno == EINTR) /* loop on EINTR */
continue;
if (errno == EWOULDBLOCK || errno == EAGAIN) {
loopIo_wantRead(ar->io);
return ASYNC_NEED_MORE;
}
return ASYNC_ERROR;
}
ar->pos += ret;
} while (ar->pos < ar->buffsize);
loopIo_dontWantRead(ar->io);
return ASYNC_COMPLETE;
}
开发者ID:Bamtau,项目名称:platform_external_qemu,代码行数:32,代码来源:async-utils.c
示例4: main
int main(int argc, char *argv[])
{
//int clifd = -1;
int len = 0;
char buf[128] = {0};
struct socket_impl sck;
if (inet_server_create(&sck, SOCK_STREAM, NULL, 5001) == -1)
return -1;
//clifd = socket_accept(sck.fd);
//printf("accept succ\n");
//printf("fd = %d\n", clifd);
sleep(1);
socket_event_add(&sck.evl, SOCKET_ON_CLOSE, on_close, NULL);
socket_event_add(&sck.evl, SOCKET_ON_RECV, on_recv, NULL);
while (sck.cli_fd[0] == -1) usleep(200);
while ((len = socket_recv(sck.cli_fd[0], buf, sizeof(buf))))
{
if (len <= 0) break;
printf("len: %d, info: %s\n", len, buf);
socket_send(sck.cli_fd[0], "hi,cli!", sizeof("hi,cli!"));
//sleep(1);
}
printf("over\n");
return 0;
}
开发者ID:antontest,项目名称:c,代码行数:29,代码来源:ser.c
示例5: http_get_file_size
int http_get_file_size(int sockfd,char* path)
{
int ret = -1;
char buf_recv_temp[BUFFER_SIZE+1];
head_to_get_file_size(path);
#ifdef HTTP_DEBUG
printf("\nsend : \n%s \n",buf_send);
#endif
socket_send(sockfd,buf_send,strlen(buf_send),0);
memset(buf_recv_temp,0,sizeof(buf_recv_temp));
ret = socket_recv(sockfd, buf_recv_temp, sizeof(buf_recv_temp)-1, 0);
D(printf("recv len = %d\n",ret));
D(printf("recv = %s\n",buf_recv_temp));
if( ret <= 0 )
{
perror("ERROR: failed to get file size");
return -1;
}
ret = http_get_content_length(buf_recv_temp);
if( ret <= 0 )
return -1;
else
return ret;
}
开发者ID:hangyan,项目名称:Code,代码行数:32,代码来源:http_download.c
示例6: meth_receive
/*-------------------------------------------------------------------------*\
* Receives data from a UDP socket
\*-------------------------------------------------------------------------*/
static int meth_receive(lua_State *L) {
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
char buf[UDP_DATAGRAMSIZE];
size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
int err;
p_timeout tm = &udp->tm;
timeout_markstart(tm);
if (!dgram) {
lua_pushnil(L);
lua_pushliteral(L, "out of memory");
return 2;
}
err = socket_recv(&udp->sock, dgram, wanted, &got, tm);
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
if (err != IO_DONE && err != IO_CLOSED) {
lua_pushnil(L);
lua_pushstring(L, udp_strerror(err));
if (wanted > sizeof(buf)) free(dgram);
return 2;
}
lua_pushlstring(L, dgram, got);
if (wanted > sizeof(buf)) free(dgram);
return 1;
}
开发者ID:AntonioModer,项目名称:luasocket,代码行数:28,代码来源:udp.c
示例7: on_recv
void on_recv(int fd, void *arg)
{
char buf[128] = {0};
int sock_type = get_socket_type(fd);
int proto_type = get_socket_protocol(fd);
switch (sock_type) {
case SOCK_STREAM:
if (proto_type == IPPROTO_TCP)
socket_recv(fd, buf, sizeof(buf));
else if (proto_type == IPPROTO_SCTP) ;
else return;
break;
case SOCK_DGRAM:
socket_addr_recvfrom(fd, buf, sizeof(buf),
(void *)&((struct socket_impl *)arg)->addr.in_addr);
break;
default:
return;
break;
}
printf("recv from %s: %s\n",
inet_ntoa(((struct socket_impl *)arg)->addr.in_addr.sin_addr),
buf);
}
开发者ID:antontest,项目名称:c,代码行数:26,代码来源:socket.c
示例8: recv_packet
void recv_packet(int sock)
{
ssize_t res;
while ((res = socket_recv(sock, &m_frame[0], m_frame.size())) >= 0) {
if (res == 0)
continue;
if (payload_hdr_is_done(m_payload_hdr)) {
stop();
return;
}
if (!payload_hdr_block_id_equal(m_payload_hdr))
continue;
if (payload_hdr_is_ack(m_payload_hdr) &&
packet_hdr_is_from_dest(m_packet_hdr)) {
m_packets = 0;
m_budget = 0;
payload_data_reset();
continue;
}
if (payload_hdr_is_enc(m_payload_hdr))
payload_data_read(m_payload_data);
if (payload_data_received() >= m_threshold)
send_budget();
if (payload_data_is_complete())
send_ack();
}
}
开发者ID:hundeboll,项目名称:raw_rlnc,代码行数:34,代码来源:helper.cpp
示例9: recv
ssize_t recv(int sock, void * p_buf, size_t buf_size, int flags)
{
VERIFY_MODULE_IS_INITIALIZED();
VERIFY_SOCKET_ID(sock);
NULL_PARAM_CHECK(p_buf);
SOCKET_MUTEX_LOCK();
socket_entry_t * p_socket_entry = &m_socket_table[sock];
SOCKET_MUTEX_UNLOCK();
ssize_t ret = -1;
if (p_socket_entry->state == STATE_CONNECTED)
{
uint32_t recv_size = 0;
uint32_t err_code = socket_recv(&p_socket_entry->handle,
p_buf,
buf_size,
&recv_size,
flags);
if (err_code == NRF_SUCCESS)
{
ret = (ssize_t) recv_size;
}
socket_set_errno(err_code);
}
else
{
set_errno(ENOTCONN);
}
return ret;
}
开发者ID:sische,项目名称:MasterThesis,代码行数:31,代码来源:socket.c
示例10: pbpal_read_over
bool pbpal_read_over(pubnub_t *pb)
{
unsigned to_read = 0;
WATCH_ENUM(pb->sock_state);
WATCH_USHORT(pb->readlen);
WATCH_USHORT(pb->left);
WATCH_UINT(pb->len);
if (pb->readlen == 0) {
int recvres;
to_read = pb->len - pbpal_read_len(pb);
if (to_read > pb->left) {
to_read = pb->left;
}
recvres = socket_recv(pb->pal.socket, (char*)pb->ptr, to_read, 0);
if (recvres <= 0) {
/* This is error or connection close, which may be handled
in some way...
*/
return false;
}
pb->sock_state = STATE_READ;
pb->readlen = recvres;
}
to_read = pb->len;
if (pb->readlen < to_read) {
to_read = pb->readlen;
}
pb->ptr += to_read;
pb->readlen -= to_read;
PUBNUB_ASSERT_OPT(pb->left >= to_read);
pb->left -= to_read;
pb->len -= to_read;
if (pb->len == 0) {
pb->sock_state = STATE_NONE;
return true;
}
if (pb->left == 0) {
/* Buffer has been filled, but the requested block has not been
* read. We have to "reset" this "mini-fsm", as otherwise we
* won't read anything any more. This means that we have lost
* the current contents of the buffer, which is bad. In some
* general code, that should be reported, as the caller could
* save the contents of the buffer somewhere else or simply
* decide to ignore this block (when it does end eventually).
*/
pb->sock_state = STATE_NONE;
}
else {
pb->sock_state = STATE_NEWDATA_EXHAUSTED;
return false;
}
return true;
}
开发者ID:evanbeard,项目名称:c-core,代码行数:59,代码来源:pbpal_sockets.c
示例11: _fbUpdatesImpl_io_callback
/*
* Asynchronous I/O callback launched when framebuffer notifications are ready
* to be read.
* Param:
* opaque - FrameBufferImpl instance.
*/
static void
_fbUpdatesImpl_io_callback(void* opaque, int fd, unsigned events)
{
FrameBufferImpl* fbi = opaque;
int ret;
// Read updates while they are immediately available.
for (;;) {
// Read next chunk of data.
ret = HANDLE_EINTR(
socket_recv(fbi->sock,
fbi->reader_buffer + fbi->reader_offset,
fbi->reader_bytes - fbi->reader_offset));
if (ret < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
// Chunk is not avalable at this point. Come back later.
return;
}
if (ret <= 0) {
/* disconnection ! */
derror("Unable to receive framebuffer data: %s\n",
ret < 0 ? strerror(errno), "unexpected disconnection");
fbUpdatesImpl_destroy();
return;
}
fbi->reader_offset += ret;
if (fbi->reader_offset != fbi->reader_bytes) {
// There are still some data left in the pipe.
continue;
}
// All expected data has been read. Time to change the state.
if (fbi->fb_state == EXPECTS_HEADER) {
// Update header has been read. Prepare for the pixels.
fbi->fb_state = EXPECTS_PIXELS;
fbi->reader_offset = 0;
fbi->reader_bytes = fbi->update_header.w *
fbi->update_header.h *
(fbi->bits_per_pixel / 8);
fbi->reader_buffer = malloc(fbi->reader_bytes);
if (fbi->reader_buffer == NULL) {
APANIC("Unable to allocate memory for framebuffer update\n");
}
} else {
// Pixels have been read. Prepare for the header.
uint8_t* pixels = fbi->reader_buffer;
fbi->fb_state = EXPECTS_HEADER;
fbi->reader_offset = 0;
fbi->reader_bytes = sizeof(FBUpdateMessage);
fbi->reader_buffer = (uint8_t*)&fbi->update_header;
// Perform the update. Note that pixels buffer must be freed there.
_update_rect(fbi->fb, fbi->update_header.x,
fbi->update_header.y, fbi->update_header.w,
fbi->update_header.h, fbi->bits_per_pixel,
pixels);
}
}
开发者ID:CriGio,项目名称:platform_external_qemu,代码行数:65,代码来源:fb-updates-impl.c
示例12: client_read_byte
static unsigned char client_read_byte(const remote_process_client *client)
{
unsigned char data;
if (0 != socket_recv(client->socket, (char*)&data, 1)) {
exit(10012);
}
return data;
}
开发者ID:Megabyteceer,项目名称:c-cgdk,代码行数:8,代码来源:remote_process_client.c
示例13: MS_OpenPort
/**
******************************************************************************
* @brief 1. 打开读卡器
* @param[in] None
* @param[out] None
*
* @retval 0 : 打开读卡器端口成功
* @retval 1 : 打开读卡器端口失败
******************************************************************************
*/
extern int
MS_OpenPort()
{
unsigned int socketfd = 0;
char buf[16] = {0};
int pos = 0;
int sendlen = 0;
int recvlen = 0;
int ret = 1;
if ((socketfd = connect_start()) == 0)
{
log_exit();
return 1;
}
memcpy(buf, the_head, 3); //3字节GET
pos += 3;
buf[pos] = 1;
pos += 1;
memcpy(buf + pos, &sendlen, sizeof(int));
pos += sizeof(int);
do
{
if ((sendlen + 8) != socket_send(socketfd, buf, sendlen + 8))
{
log_print("%s发送数据数据出错\n", __FUNCTION__);
break;
}
memset(buf, 0x00, sizeof(buf));
recvlen = socket_recv(socketfd, buf, 12); //len = 8 + 4
if ((recvlen == -1) || (recvlen != 12))
{
log_print("%s接收数据出错[recvlen:%d]\n", __FUNCTION__, recvlen);
break;
}
pos = 0;
if ((memcmp(buf, "PUT", 3) != 0) || (buf[3] != 1))
{
log_print("%s 头不合法\n", __FUNCTION__);
break;
}
pos += 4;
memcpy(&recvlen, buf + pos, sizeof(int));
if (recvlen != sizeof(int))
{
log_print("%s 长度不合法\n", __FUNCTION__);
break;
}
pos += 4;
memcpy(&ret, buf + pos, sizeof(int));
ret = (ret == 0) ? ret : 1;
} while(0);
connect_close(socketfd);
return ret;
}
开发者ID:liuning587,项目名称:MStation,代码行数:67,代码来源:MStation.c
示例14: client_getc
int client_getc (client_t *c)
{
uint8_t b;
if (socket_recv (c->sock, &b, 1) <= 0) {
// No data available
return -1;
}
return b;
}
开发者ID:AtomSoftTech,项目名称:retrobsd,代码行数:10,代码来源:client.c
示例15: ReadMessage
int ReadMessage( unsigned int* sock, int count , char* buffer , int*len , int*errorid , int timeout )
{
int error = 0;
fd_set set;
FD_ZERO(&set);
for( int i=0; i<count; i++ )
if( sock[i] != 0 )
FD_SET( sock[i] , &set);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = timeout*1000;
int num = select( FD_SETSIZE , &set, NULL, NULL, &tv);
if (num > 0)
{
for( int i=0; i<count; i++ )
{
if( sock[i] && FD_ISSET( sock[i] , &set ) )
{
int length;
TraceMsg( "to receive %d\n" , i );
if( socket_recv( sock[i], (char*)&length, sizeof(length)) == 0)
{
if ( socket_recv( sock[i], buffer , length) == 0)
{
*len = length;
return i;
}
else
error = -1;
}
else
error = -2;
if( error )
*errorid = i;
break;
}
}
}
else if( num < 0 )
error = -3;
return error;
}
开发者ID:bgtwoigu,项目名称:Innov_code,代码行数:42,代码来源:NetApi.cpp
示例16: pbpal_line_read_status
enum pubnub_res pbpal_line_read_status(pubnub_t *pb)
{
uint8_t c;
if (pb->readlen == 0) {
int recvres = socket_recv(pb->pal.socket, (char*)pb->ptr, pb->left, 0);
if (recvres < 0) {
if (socket_timed_out()) {
return PNR_TIMEOUT;
}
if (PUBNUB_BLOCKING_IO_SETTABLE && pb->options.use_blocking_io) {
return PNR_IO_ERROR;
}
return socket_would_block() ? PNR_IN_PROGRESS : PNR_IO_ERROR;
}
else if (0 == recvres) {
return PNR_TIMEOUT;
}
PUBNUB_LOG_TRACE("have new data of length=%d: %s\n", recvres, pb->ptr);
pb->sock_state = STATE_READ_LINE;
pb->readlen = recvres;
}
while (pb->left > 0 && pb->readlen > 0) {
c = *pb->ptr++;
--pb->readlen;
--pb->left;
if (c == '\n') {
int read_len = pbpal_read_len(pb);
PUBNUB_LOG_TRACE("\n found: "); WATCH_INT(read_len); WATCH_USHORT(pb->readlen);
pb->sock_state = STATE_NONE;
return PNR_OK;
}
}
if (pb->left == 0) {
/* Buffer has been filled, but new-line char has not been
* found. We have to "reset" this "mini-fsm", as otherwise we
* won't read anything any more. This means that we have lost
* the current contents of the buffer, which is bad. In some
* general code, that should be reported, as the caller could
* save the contents of the buffer somewhere else or simply
* decide to ignore this line (when it does end eventually).
*/
pb->sock_state = STATE_NONE;
}
else {
pb->sock_state = STATE_NEWDATA_EXHAUSTED;
}
return PNR_IN_PROGRESS;
}
开发者ID:evanbeard,项目名称:c-core,代码行数:54,代码来源:pbpal_sockets.c
示例17: fcgi_buf_socket_recv
/*******************************************************************************
* Read from an open file descriptor into buffer.
*
* The caller should disable the default Apache SIGPIPE handler,
* otherwise a bad script could cause the request to abort and appear
* as though the client's fd caused it.
*
* Results:
* <0 error, errno is set
* =0 EOF reached
* >0 successful read or no room in buffer (NOT # of bytes read)
*/
int fcgi_buf_socket_recv(Buffer *buf, SOCKET fd)
{
int len;
fcgi_buf_check(buf);
if (buf->length == buf->size)
/* there's no room in the buffer, return "success" */
return 1;
if (buf->length == 0)
/* the buffer is empty so defrag */
buf->begin = buf->end = buf->data;
len = min(buf->size - buf->length, buf->data + buf->size - buf->end);
#ifndef NO_WRITEV
/* assume there is a readv() since there is a writev() */
if (len == buf->size - buf->length)
{
#endif
len = socket_recv(fd, buf->end, len);
#ifndef NO_WRITEV
}
else
{
/* the buffer is wrapped, use readv() */
struct iovec vec[2];
vec[0].iov_base = buf->end;
vec[0].iov_len = len;
vec[1].iov_base = buf->data;
vec[1].iov_len = buf->size - buf->length - len;
ASSERT(len);
ASSERT(vec[1].iov_len);
do
{
len = readv(fd, vec, 2);
}
while (len == -1 && errno == EINTR);
}
#endif
if (len <= 0) return len;
fcgi_buf_added(buf, len);
return len; /* this may not contain the number of bytes read */
}
开发者ID:AzerTyQsdF,项目名称:osx,代码行数:66,代码来源:fcgi_buf.c
示例18: socket_recv
bool DrainerObject::drainSocket() {
errno = 0;
char buff[1024];
int size = socket_recv(mSocket, buff, sizeof(buff));
if (size > 0) {
return true;
} else if (size < 0 && errno == EWOULDBLOCK) {
return true;
}
mSocketIsDrained = true;
return false;
}
开发者ID:ashishb,项目名称:android_emulator,代码行数:12,代码来源:SocketDrainer.cpp
示例19: udp_read_packet
/*
* Read a received packet into buffer buf (which is of maximum length len);
* store calling ip and port as well. Call available() to make sure data is
* ready first.
* NOTE: I don't believe len is ever checked in implementation of recvfrom(),
* so it's easy to overflow buffer. so we check and truncate.
* Returns number of bytes read, or negative number of bytes we would have
* needed if we truncated.
*/
int udp_read_packet (udp_t *u, uint8_t *buf, unsigned len,
uint8_t *ip, unsigned *port)
{
int nbytes = udp_available (u) - 8; /* skip UDP header */
if (nbytes < 0) {
/* No real data here. */
return 0;
}
if (nbytes > (int)len) {
/* Packet is too large - truncate.
* HACK: hand-parse the UDP packet using TCP recv method. */
uint8_t tmpBuf[8];
int i;
/* Read 8 header bytes and get IP and port from it. */
socket_recv (u->sock, tmpBuf, 8);
if (ip != 0) {
ip[0] = tmpBuf[0];
ip[1] = tmpBuf[1];
ip[2] = tmpBuf[2];
ip[3] = tmpBuf[3];
}
if (port != 0)
*port = (tmpBuf[4] << 8) + tmpBuf[5];
/* Now copy first (len) bytes into buf. */
for (i=0; i<(int)len; i++) {
socket_recv (u->sock, tmpBuf, 1);
buf[i] = tmpBuf[0];
}
/* And just read the rest byte by byte and throw it away. */
while (udp_available (u)) {
socket_recv (u->sock, tmpBuf, 1);
}
return -nbytes;
}
return socket_recvfrom (u->sock, buf, len, ip, port);
}
开发者ID:denrusio,项目名称:vak-opensource,代码行数:49,代码来源:udp.c
示例20: netPipe_recvBuffers
static int
netPipe_recvBuffers( void* opaque, GoldfishPipeBuffer* buffers, int numBuffers )
{
NetPipe* pipe = opaque;
int count = 0;
int ret = 0;
int buffStart = 0;
GoldfishPipeBuffer* buff = buffers;
GoldfishPipeBuffer* buffEnd = buff + numBuffers;
for (; buff < buffEnd; buff++)
count += buff->size;
buff = buffers;
while (count > 0) {
int avail = buff->size - buffStart;
int len = socket_recv(pipe->io->fd, buff->data + buffStart, avail);
/* the read succeeded */
if (len > 0) {
buffStart += len;
if (buffStart >= buff->size) {
buff++;
buffStart = 0;
}
count -= len;
ret += len;
continue;
}
/* we reached the end of stream? */
if (len == 0) {
if (ret == 0)
ret = PIPE_ERROR_IO;
break;
}
/* if we already read some stuff, simply return */
if (ret > 0) {
break;
}
/* need to return an appropriate error code */
if (errno == EAGAIN || errno == EWOULDBLOCK) {
ret = PIPE_ERROR_AGAIN;
} else {
ret = PIPE_ERROR_IO;
}
break;
}
return ret;
}
开发者ID:Jib-BAOSP,项目名称:platform_external_qemu,代码行数:52,代码来源:hw-pipe-net.c
注:本文中的socket_recv函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论