|
MASA-Core
|
00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2010-2015 Edans Sandes 00004 * 00005 * This file is part of MASA-Core. 00006 * 00007 * MASA-Core is free software: you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation, either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * MASA-Core is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with MASA-Core. If not, see <http://www.gnu.org/licenses/>. 00019 * 00020 ******************************************************************************/ 00021 00022 #include "SocketCellsWriter.hpp" 00023 00024 #include <stdio.h> 00025 #include <string.h> 00026 #include <unistd.h> 00027 #include <stdlib.h> 00028 00029 #include <sys/socket.h> /* for socket(), bind(), and connect() */ 00030 #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ 00031 #include <errno.h> 00032 00033 #define DEBUG (0) 00034 00035 SocketCellsWriter::SocketCellsWriter(string hostname, int port) { 00036 this->hostname = hostname; 00037 this->port = port; 00038 this->socketfd = -1; 00039 init(); 00040 } 00041 00042 SocketCellsWriter::~SocketCellsWriter() { 00043 close(); 00044 } 00045 00046 void SocketCellsWriter::close() { 00047 fprintf(stderr, "SocketCellsWriter::close(): %d\n", socketfd); 00048 if (socketfd != -1) { 00049 ::close(socketfd); 00050 socketfd = -1; 00051 } 00052 } 00053 00054 int SocketCellsWriter::write(const cell_t* buf, int len) { 00055 int ret = send(socketfd, buf, len*sizeof(cell_t), 0); 00056 if (ret == -1) { 00057 fprintf(stderr, "send: Socket error: -1\n"); 00058 return 0; 00059 } 00060 return ret; 00061 } 00062 00063 void SocketCellsWriter::init() { 00064 int rc; 00065 int servSock; /* Socket descriptor for server */ 00066 int clntSock; /* Socket descriptor for client */ 00067 struct sockaddr_in echoServAddr; /* Local address */ 00068 struct sockaddr_in echoClntAddr; /* Client address */ 00069 unsigned int clntLen; /* Length of client address data structure */ 00070 00071 if (DEBUG) printf("SocketCellsWriter: create socket\n"); 00072 /* Create socket for incoming connections */ 00073 if ((servSock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { 00074 fprintf(stderr, "ERROR creating server socket; return code from socket() is %d\n", servSock); 00075 exit(-1); 00076 } 00077 00078 int optval = 1; 00079 setsockopt(servSock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); 00080 00081 00082 00083 /* Construct local address structure */ 00084 memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ 00085 echoServAddr.sin_family = AF_INET; /* Internet address family */ 00086 echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ 00087 echoServAddr.sin_port = htons(port); /* Local port */ 00088 00089 /* Bind to the local address */ 00090 if (DEBUG) printf("SocketCellsWriter: Bind to local address %d\n", port); 00091 if ((rc = bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0) { 00092 fprintf(stderr, "ERROR; return code from bind() is %d\n", rc); 00093 exit(-1); 00094 } 00095 00096 /* Mark the socket so it will listen for incoming connections */ 00097 if (DEBUG) printf("SocketCellsWriter: Listening on port %d\n", port); 00098 if ((rc=listen(servSock, 1)) < 0) { 00099 fprintf(stderr, "ERROR; return code from listen() is %d\n", rc); 00100 exit(-1); 00101 } 00102 00103 /* Set the size of the in-out parameter */ 00104 clntLen = sizeof(echoClntAddr); 00105 00106 /* Wait for a client to connect */ 00107 if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0){ 00108 fprintf(stderr, "ERROR; return code from accept() is %d\n", clntSock); 00109 exit(-1); 00110 } 00111 00112 /* clntSock is connected to a client! */ 00113 00114 fprintf(stderr, "Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); 00115 00116 //HandleTCPClient(clntSock); 00117 00118 ::close(servSock); 00119 00120 this->socketfd = clntSock; 00121 }
1.7.6.1