-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdata_source_tcp_server.cpp
More file actions
83 lines (73 loc) · 1.56 KB
/
data_source_tcp_server.cpp
File metadata and controls
83 lines (73 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "data_source_tcp_server.h"
data_source_tcp_server::data_source_tcp_server( int portno )
{
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd >= 0)
{
int flag;
flag = 1;
if( setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *) &flag,sizeof(int) )< 0 )
{
printf("Unable to set TCP_NODELAY\n");
}
flag = 1;
if( setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *) &flag,sizeof(int) )< 0 )
{
printf("Unable to set SO_REUSEADDR\n");
}
memset((char *) &serv_addr, 0x00, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
{
listen(sockfd,1);
clilen = sizeof(cli_addr);
printf("Listening on port %i\n",portno);
fd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (fd >= 0)
{
printf("Server connected port %i\n",portno);
}
else
{
printf("ERROR on accept\n");
}
}
else
{
printf("ERROR on binding\n");
}
}
else
{
printf("ERROR opening socket\n");
}
}
data_source_tcp_server::~data_source_tcp_server()
{
if( fd != -1 )
{
close( fd );
}
if( sockfd != -1 )
{
close(sockfd);
}
}
void data_source_tcp_server::write( const uint8_t * data, size_t bytes )
{
::write( fd, data, bytes );
}