Last active
January 11, 2017 13:43
-
-
Save rinevo/d09501fc37e667183eacca1d68bda019 to your computer and use it in GitHub Desktop.
socket通信とselectによる多重化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <iostream> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#include "NetworkCommon.h" | |
#include "port_list.h" | |
int main(int argc, char *argv[]) | |
{ | |
if (argc < 3) { | |
std::cerr << "arg error" << std::endl; | |
return -1; | |
} | |
int iPortNo = atoi(argv[1]); | |
CNetworkCommon net; | |
int iSock = net.ClientSocket( | |
port_list[iPortNo].socket_family, | |
port_list[iPortNo].socket_type, | |
port_list[iPortNo].port); | |
if (iSock < 0) { | |
return -1; | |
} | |
char buf[512]; | |
strcpy(buf, argv[2]); | |
std::cout << "send[" << iPortNo << "]=" << buf << std::endl; | |
write(iSock, buf, strlen(buf)); | |
close(iSock); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile | |
CC = g++ | |
CFLG = -I../common | |
all: | |
$(CC) -c *.cpp $(CFLG) | |
$(CC) -o server server.o NetworkCommon.o | |
$(CC) -o client client.o NetworkCommon.o | |
clean: | |
rm *.o | |
rm server | |
rm client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "NetworkCommon.h" | |
#include <iostream> | |
#include <sys/un.h> | |
#include <unistd.h> | |
using namespace std; | |
CNetworkCommon::CNetworkCommon() | |
{ | |
} | |
CNetworkCommon::~CNetworkCommon() | |
{ | |
} | |
int CNetworkCommon::ServerSocket(int socket_family, int socket_type, std::string port) | |
{ | |
int iSock; | |
struct sockaddr_un addr; | |
iSock = socket(socket_family, socket_type, 0); | |
if (iSock < 0) { | |
std::cerr << "socket fail" << std::endl; | |
return -1; | |
} | |
unlink(port.c_str()); | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = socket_family; | |
strcpy(addr.sun_path , port.c_str()); | |
if (bind(iSock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | |
std::cerr << "bind fail" << std::endl; | |
return -1; | |
} | |
if (listen(iSock, 5) < 0) { | |
std::cerr << "listen fail" << std::endl; | |
return -1; | |
} | |
return iSock; | |
} | |
int CNetworkCommon::ClientSocket(int socket_family, int socket_type, std::string port) | |
{ | |
struct sockaddr_un addr; | |
int iSock; | |
iSock = socket(socket_family, socket_type, 0); | |
if (iSock < 0) { | |
std::cerr << "socket fail" << std::endl; | |
return -1; | |
} | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = socket_family; | |
strcpy(addr.sun_path, port.c_str()); | |
if (connect(iSock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | |
std::cerr << "connect fail" << std::endl; | |
return -1; | |
} | |
return iSock; | |
} | |
int CNetworkCommon::Recv() | |
{ | |
return 0; | |
} | |
int CNetworkCommon::Send() | |
{ | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
class CNetworkCommon | |
{ | |
public: | |
CNetworkCommon(); | |
~CNetworkCommon(); | |
int ServerSocket(int socket_family, int socket_type, std::string port); | |
int ClientSocket(int socket_family, int socket_type, std::string port); | |
int Recv(); | |
int Send(); | |
private: | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// port_list.h | |
#include <string> | |
#include <sys/un.h> | |
#define PORT_LIST_MAX 3 | |
enum { | |
PORT_LIST_A, | |
PORT_LIST_B, | |
PORT_LIST_C, | |
}; | |
typedef struct _port_list { | |
int socket_family; | |
int socket_type; | |
std::string port; | |
} port_list_t; | |
port_list_t port_list[] = { | |
AF_UNIX, SOCK_STREAM, "/tmp/port1.socket", | |
AF_UNIX, SOCK_STREAM, "/tmp/port2.socket", | |
AF_UNIX, SOCK_STREAM, "/tmp/port3.socket", | |
0, 0, "", | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include "NetworkCommon.h" | |
#include "port_list.h" | |
int main() | |
{ | |
CNetworkCommon net; | |
int iSock[PORT_LIST_MAX]; | |
fd_set fds, readfds; | |
int iFdWidth = 0; | |
char buf[512]; | |
for (int i = 0; port_list[i].port != ""; ++i) { | |
iSock[i] = -1; | |
} | |
for (int i = 0; port_list[i].port != ""; ++i) { | |
iSock[i] = net.ServerSocket( | |
port_list[i].socket_family, | |
port_list[i].socket_type, | |
port_list[i].port); | |
if (iSock[i] < 0) { | |
for (int i = 0; port_list[i].port != ""; ++i) { | |
if (iSock[i] != -1) { | |
close(iSock[i]); | |
} | |
} | |
return -1; | |
} | |
} | |
FD_ZERO(&readfds); | |
for (int i = 0; port_list[i].port != ""; ++i) { | |
FD_SET(iSock[i], &readfds); | |
if (iFdWidth < iSock[i]) { | |
iFdWidth = iSock[i]; | |
} | |
} | |
iFdWidth++; | |
sockaddr_un client; | |
socklen_t client_len = sizeof(client); | |
//for (;;) { | |
for (int z = 0; z < 3; ++z) { | |
memcpy(&fds, &readfds, sizeof(fd_set)); | |
switch (select(iFdWidth, &fds, NULL, NULL, NULL)) { | |
case 0: | |
std::cout << "select 0" << std::endl; | |
continue; | |
case -1: | |
std::cerr << "select fail" << std::endl; | |
continue; | |
} | |
for (int i = 0; port_list[i].port != ""; ++i) { | |
if (FD_ISSET(iSock[i], &fds)) { | |
int iAccept = accept(iSock[i], (struct sockaddr *)&client, &client_len); | |
memset(buf, 0, sizeof(buf)); | |
int iLen = read(iAccept, buf, sizeof(buf) - 1); | |
std::cout << "i=" << i << ", iLen=" << iLen << ", buf=" << buf << ", errno=" << errno << std::endl; | |
} | |
} | |
} | |
for (int i = 0; port_list[i].port != ""; ++i) { | |
close(iSock[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment