I create a TCP server, I always get following error when I was run. How I get rid of this error, I do not understand where wrong?
Also it say data received while connection refused, I do not run client. Why?
Error:
Socket Created
Error connect to server: Connection refused
Error to accept: Invalid argument
Data received
Server
#include <iostream>
#include "serverh.hpp"
#include "glimpsepackages.h"
#include "soupbintcppackages.h"
int main() {
//soupBinTCP structures
DebugPacket D; //"+"
LoginAcceptedPacket A;
LoginRejectPacket J;
SequencedDataPacket S;
ServerHeartBeatPacket H;
EndOfSessionPacket Z;
//glimpse structures
SecondsMessage T;
CombinationOrderBookDelivery M;
TickSizeTableEntryPackage L;
OrderBookDirectoryPackage R;
OrderBookStatePackage O;
AddOrderMessageNoMPIDPackage A1;
AddOrderMessageMPIDPackage F;
std::string ip="127.0.0.1";
int port=7888;
ssize_t size=1024;
void *data={};
TcpServer server(ip, port);
server.connectToClient();
server.accept();
for (;;) {
server.receivingData(size);
switch (server.pop()) {
case '+':
// D.length=server.pop();
D.code = server.pop8();
D.text = server.pop();
break;
case 'A' :
A.to_little_endian();
A.code = server.pop8();
A.sequence_number = server.pop32();
for (int i = 0; i < 10; i++) {
A.session[i] = server.pop();
}
break;
case 'J':
J.code = server.pop8();
J.rejectreasoncode = server.pop();
break;
case 'S':
S.code = server.pop8();
S.message = server.pop();
break;
case 'H':
H.code = server.pop8();
case 'Z':
Z.code = server.pop8();
default:
return 0;
}
}
serverh.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string>
#include "serverh.hpp"
#include "util.h"
#include <iostream>
TcpServer::TcpServer(std::string ip, int port):_ip(ip),
_port(port), _addrSize(sizeof _serverAddress)
{
_sockFd=-1;
};
bool TcpServer::connectToClient(){
_sockFd=socket(AF_INET,SOCK_STREAM,0);
if(_sockFd==-1)
perror("Error creating socket");
else
std::cerr<<"Socket Created\n";
_serverAddress.sin_addr.s_addr=inet_addr(_ip.c_str());
_serverAddress.sin_family=AF_INET;
_serverAddress.sin_port=htons(_port);
if(::connect(_sockFd,(struct sockaddr *)&_serverAddress, sizeof(_serverAddress))<0) {
perror("Error connect to server");
return false;
}
else
std::cerr<<"Connected\n";
if(::bind(_sockFd,(struct sockaddr *)&_serverAddress,_addrSize)<0)
perror("Error bind to server");
else
std::cerr<<"Socket bind\n";
if(::listen(_sockFd,5)==0)
std::cerr<<"Listining\n";
else
perror("Error on Listening");
return true;
}
bool TcpServer::accept(){
if(::accept(_sockFd,(struct sockaddr *) &_serverAddress, &_addrSize)<0)
perror("Error to accept");
else
std::cerr<<"Accepted\n";
return true;
}
void *TcpServer::receivingData(ssize_t size){
char *buffer=new char[size];
if(::recv(_sockFd,buffer, size,0)>0);
std::cerr<<"Data received\n";
return buffer;
}
void TcpServer::incOffset(int rest_length) {
void *restbuff = alloca(rest_length);
if (::recv(_sockFd, restbuff, rest_length, 0) < 0)
std::cerr << "Data received\n";
}
char TcpServer::pop(){
char ch;
if(::recv(_sockFd, &ch, 1, 0)>0)
return ch;
}
int8_t TcpServer::pop8() {
int8_t ch;
if (::recv(_sockFd, &ch, 8, 0) > 0)
return getLeValue(ch); // func include endian functions
}
int16_t TcpServer::pop16(){
int16_t ch;
if(::recv(_sockFd, &ch, 16, 0)>0)
return getLeValue(ch);
}
int32_t TcpServer::pop32(){
int32_t ch;
if(::recv(_sockFd, &ch, 32, 0)>0)
return getLeValue(ch);
}
int64_t TcpServer::pop64() {
int64_t ch;
if (::recv(_sockFd, &ch, 64, 0) > 0)
return getLeValue(ch);
}
Serverh.hpp
#ifndef SERVERH_H
#define SERVERH_H
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
class TcpServer{
public:
TcpServer(const std::string _ip,int _port);
bool connectToClient();
bool accept();
void *receivingData(ssize_t size);
bool sendToClient();
char pop();
int8_t pop8();
int16_t pop16();
int32_t pop32();
int64_t pop64();
void incOffset(int rest_length);
private:
std::string _ip;
int _port;
int _sockFd;
sockaddr_in _serverAddress;
socklen_t _addrSize;
};
#endif
The problem
Error: Socket Created Error connect to server: Connection refused Error to accept: Invalid argument Data received
Two errors here, a failure to connect because the other address is not accepting connections and a failure to accept because one or more of the input parameters are bad.
The failure to connect is understandable. Clients are rarely listening on a port for connections. Clients usually initiate the connection.
The failure to accept is caused by connectToClient exiting after failing to connect. It never binds and listens, so the socket has not been set up for accept.
Fixing this
A server should not attempt to connect. It should listen for attempts to connect to it. This makes connectToClient bizarre. It tries to connect to a client and then, on the same socket, bind and listen. You can't do both, so I'd discard
if(::connect(_sockFd,(struct sockaddr *)&_serverAddress, sizeof(_serverAddress))<0) {
perror("Error connect to server");
return false;
}
else
std::cerr<<"Connected\n";
and
reconsider
_serverAddress.sin_addr.s_addr=inet_addr(_ip.c_str());
as this will only listen to one incoming address which might be what you want, but isn't usually. I would also rename the function to reflect the change in purpose.
Note:
if(::accept(_sockFd,(struct sockaddr *) &_serverAddress, &_addrSize)<0)
passes in a pointer to the server address. This location will be overwritten with the address of the client. It's harmless, but you probably don't want to do this. You aren't using the returned address as far as I can see, so you might as well just pass in NULLs and pass on it.
if(::accept(_sockFd, NULL, NULL)<0)
Related
I'm new to socket programming and wanted to try something simple. This program can manipulate settings on my tv. All messages are 24 bytes. There may be one or more messages returned. I cannot figure out a good solution to get all of the messages without read() blocking on me.
What is below would be what I hoped to be a simple solution. It seems to work in a lot of example code I have found. However, what happens is after the first loop it seems to just block on the read() operation infinitely. If I remove the loop and just put multiple reads, the same thing happens. As long as I don't try to read more information that is sent, I'm ok.
I did try a couple of other things like turning off blocking, and adding a timer. neither worked. At this point I can live with a couple seconds of blocking. I just want the program to exit normally after the read.
adding output for a power_on command. It correctly outputs the two lines it should then blocks indefinitely.
Dans-MBP:~ mreff555$ ./tvthing
24: *SAPOWR0000000000000000
24: *SNPOWR0000000000000001
code below:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/time.h>
#define PORT 20060
#define POWER_ON "*SCPOWR0000000000000001\n"
#define POWER_OFF "*SCPOWR0000000000000000\n"
#define POWER_STATUS "*SEPOWR################\n"
#define POWER_TOGGLE "*STPOWR################\n"
int main(int argc, char const * argv[])
{
struct sockaddr_in tvAddress;
struct hostent *host = gethostbyname("192.168.1.128");
memset(&tvAddress, 0, sizeof(tvAddress));
tvAddress.sin_family = AF_INET;
tvAddress.sin_addr.s_addr = htonl(INADDR_ANY);
tvAddress.sin_addr.s_addr = ((struct in_addr*)(host->h_addr))->s_addr;
tvAddress.sin_port = htons(PORT);
char sendBuffer[24] = {0};
char recBuffer[24] = {0};
int socket_fd;
if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
else
{
if(connect(socket_fd, (struct sockaddr *)&tvAddress, sizeof(struct sockaddr)))
{
perror("connection failed failed");
exit(EXIT_FAILURE);
}
memcpy(&sendBuffer, &POWER_STATUS, sizeof(sendBuffer));
write(socket_fd, sendBuffer, strlen(sendBuffer));
int ret;
while((ret = read(socket_fd, recBuffer, sizeof(recBuffer)) > 0))
{
printf("%d: %s\n", ret, recBuffer);
}
close(socket_fd);
}
}
You need to read until your buffer is full like this:
unsigned readLen = 0;
unsigned totalLen = sizeof(recBuffer);
while (readLen < totalLen) {
int ret = read(socket_fd, recBuffer + readLen, totalLen - readLen);
if (ret > 0) {
readLen += ret;
} else {
// error handling here
break;
}
}
This is needed because read() returns only the currently available amount of bytes which might be less than you have requested. From the corresponding man-page:
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.
If you need to receive several responses you can put the described algorithm into a function and use it repeatedly. In any case you need to know how many responses to expect otherwise your read() will block because it seems that your TV's server is programmed to keep the connection open and it is client's responsibility to choose when to disconnect.
If you decide to make your application more sophisticated you can use one of the IO Multiplexing mechanisms to make your wait for response interruptable by timer or terminal input. For example:
while (true) {
pollfd fds[] = {
{ socket_fd, POLLIN, 0 },
{ STDIN_FILENO, POLLIN, 0 }
};
int ret = poll(fds, sizeof(fds) / sizeof(*fds), -1);
if (ret > 0) {
if (fds[0].revents & POLLIN) {
readResponse(); // read and process response
}
if (fds[1].revents & POLLIN) {
break; // exit on terminal input
}
}
}
As it turns out, select is designed exactly for that purpose. It checks the specified file descriptors for a specified time interval, and if successful repeats the process. Tweaking the time interval minimizes the blocking while allowing enough time for additional messages to come in.
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#define PORT 20060
#define POWER_ON "*SCPOWR0000000000000001\n"
#define POWER_OFF "*SCPOWR0000000000000000\n"
#define POWER_STATUS "*SEPOWR################\n"
#define POWER_TOGGLE "*STPOWR################\n"
int main(int argc, char const * argv[])
{
struct sockaddr_in tvAddress;
struct hostent *host = gethostbyname("192.168.1.128");
memset(&tvAddress, 0, sizeof(tvAddress));
tvAddress.sin_family = AF_INET;
tvAddress.sin_addr.s_addr = htonl(INADDR_ANY);
tvAddress.sin_addr.s_addr = ((struct in_addr*)(host->h_addr))->s_addr;
tvAddress.sin_port = htons(PORT);
char sendBuffer[24] = {0};
char recBuffer[24] = {0};
int socket_fd;
if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
else
{
if(connect(socket_fd, (struct sockaddr *)&tvAddress, sizeof(struct sockaddr)))
{
perror("connection failed failed");
exit(EXIT_FAILURE);
}
struct timeval tv;
fd_set sockRead;
int selectStatus;
memcpy(&sendBuffer, &POWER_ON, sizeof(sendBuffer));
write(socket_fd, sendBuffer, strlen(sendBuffer));
do
{
FD_ZERO(&sockRead);
FD_SET(socket_fd, &sockRead);
tv.tv_sec = 2;
tv.tv_usec = 500000;
selectStatus = select(socket_fd + 1, &sockRead, NULL, NULL, &tv);
switch(selectStatus)
{
case -1:
perror("select()");
exit(EXIT_FAILURE);
break;
case 0:
break;
default:
printf("Ready for Reading\n");
read(socket_fd, recBuffer, sizeof(recBuffer));
printf("%s\n", recBuffer);
}
}while (selectStatus > 0);
close(socket_fd);
}
}
I am working on a primitive multiplayer battleship game for my CS class. I am able to get it running and send out information over LAN. To test whether this communication is working, I created a loop with a listener function that will stop when the packet is verified to have been received from the other computer's listen thread.
When I do this, it seems to send the packet, but the loop keeps running forever without any errors and without receiving confirmation that the packets were received by the other computer. Do you have any idea what might be happening?
//SFML headers
#include <SFML/Network.hpp>
//standard headers
#include <vector>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <exception>
std::atomic_bool running;
unsigned short port;
//Functions for threading
void startListen(sf::UdpSocket *sock, std::atomic_bool *r, sf::IpAddress *ip)
{
std::cout<<"Listener started"<<std::endl;
while(*r == true)
{
sf::sleep(sf::microseconds(50));
std::cout<<"Wait done"<<std::endl;
std::cout<<std::to_string(port)<<" port from the listen thread."<<std::endl;
sf::IpAddress sender;
sf::Packet p;
std::cout<<"tryna receive packet"<<std::endl;
if (sock->receive(p, sender, port) != sf::Socket::Done)
{
std::cout<<"Attempt at packet retrieval but packet retrieval failed! Error code 23./nPlease contact developer at mldevelopingstudios#gmail.com to report this failure."<<std::endl;
}
std::cout<<"Packet received!"<<std::endl;
std::string type;
p >> type;
std::cout<<"packet recieved, type: "<<type<<std::endl;
}
}
int main()
{
port = 4343;
sf::UdpSocket sock;
sf::UdpSocket recSock;
if (recSock.bind(port) != sf::Socket::Done)
{
std::cout<<"Whoops we crashed!!!!!!!!!!";
return 0;
}
//sock.unbind();
std::string ip;
sf::IpAddress IP1 = sf::IpAddress::getLocalAddress();
sf::String ip11 = IP1.toString();
std::cout<<"You LAN IP is: "<<ip11.toAnsiString()<<std::endl;
std::cout<<"Please enter the target IP address of the computer you'd like to play with."<<std::endl;
std::string iiii;
std::cin>>iiii;
sf::String o(iiii);
ip = o;
sf::IpAddress IP(ip);
std::thread listen(startListen, &recSock, &running, &IP);
listen.detach();
std::cout<<"Connection check"<<std::endl;
running = true;
std::string name;
std::cout<<"Name? "<<std::endl;
std::cin>>name;
while(running == true)
{
std::string mess = name;
sf::Packet packk;
std::string temp;
std::cout<<"Message: "<<std::endl;
std::cin>>temp;
if(temp == "!!STOP!!")
{
running = false;
}
else
{
mess = mess+ ": "+ temp;
packk<<mess;
if(sock.send(packk, IP, port)!= sf::Socket::Done)
{
std::cout<<"Sending error in test loop!!!!"<<std::endl;
}
}
}
recSock.unbind();
return 0;
}
Any help is greatly appreciated,
thank you and have a nice day.
EDIT: New code is the "minimalist code" and still no errors but nothing is received on either end.
EDIT: Added code to show whats happening
//SFML headers
#include <SFML/Network.hpp>
//standard headers
#include <vector>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <exception>
std::atomic_bool running;
unsigned short port;
//Functions for threading
void startListen(sf::UdpSocket *sock, std::atomic_bool *r, sf::IpAddress *ip)
{
std::cout<<"Listener started"<<std::endl;
while(*r == true)
{
unsigned short por = 4343;
sock->setBlocking(false);
std::cout<<std::to_string(por)<<" port from the listen thread."<<std::endl;
sf::IpAddress sender;
sf::Packet p;
std::cout<<"tryna receive packet"<<std::endl;
sf::Socket::Status stat = sock->receive(p, sender, por);
switch(stat)
{
case(sf::Socket::Done):
{
std::cout<<"Packet receiving completed!"<<std::endl;
std::cout<<"Packet received!"<<std::endl;
std::string type;
p >> type;
std::cout<<"packet recieved, type: "<<type<<std::endl;
break;
}
case(sf::Socket::NotReady):
std::cout<<"Socket not ready to received!"<<std::endl;
break;
case(sf::Socket::Partial):
std::cout<<"socket Kinda received?"<<std::endl;
break;
case(sf::Socket::Disconnected):
std::cout<<"Socket disconnected!"<<std::endl;
break;
case(sf::Socket::Error):
std::cout<<"Errorrrrrrrrrrrrrrrrrrr"<<std::endl;
break;
default:
std::cout<<"Oh noooooo"<<std::endl;
break;
}
}
}
int main()
{
port = 4343;
sf::UdpSocket sock;
sf::UdpSocket recSock;
if (recSock.bind(port) != sf::Socket::Done)
{
std::cout<<"Whoops we crashed!!!!!!!!!!";
return 0;
}
std::string ip;
sf::IpAddress IP1 = sf::IpAddress::getLocalAddress();
sf::String ip11 = IP1.toString();
std::cout<<"You LAN IP is: "<<ip11.toAnsiString()<<std::endl;
std::cout<<"Please enter the target IP address of the computer you'd like to play with."<<std::endl;
std::string iiii;
std::cin>>iiii;
sf::String o(iiii);
ip = o;
sf::IpAddress IP(ip);
std::thread listen(startListen, &recSock, &running, &IP);
listen.detach();
std::cout<<"Connection check"<<std::endl;
running = true;
std::string name;
std::cout<<"Name? "<<std::endl;
std::cin>>name;
while(running == true)
{
std::string mess = name;
sf::Packet packk;
std::string temp;
std::cout<<"Message: "<<std::endl;
std::cin>>temp;
if(temp == "!!STOP!!")
{
running = false;
}
else
{
mess = mess+ ": "+ temp;
packk<<mess;
sf::Socket::Status stat = sock.send(packk, IP, port);
switch(stat)
{
case(sf::Socket::Done):
std::cout<<"Packet Sending completed!"<<std::endl;
break;
case(sf::Socket::NotReady):
std::cout<<"Socket not ready to send!"<<std::endl;
break;
case(sf::Socket::Partial):
std::cout<<"socket Kinda sent?"<<std::endl;
break;
case(sf::Socket::Disconnected):
std::cout<<"Socket disconnected!"<<std::endl;
break;
case(sf::Socket::Error):
std::cout<<"Errorrrrrrrrrrrrrrrrrrr"<<std::endl;
break;
default:
std::cout<<"Oh noooooo"<<std::endl;
}
}
}
recSock.unbind();
return 0;
}
The socket on the receiving end is constantly reporting sf::Socket::NotReady
The sending reports sf::Socket::Done
Nothing is received on either end, when blocking is set to true or false
I somehow solved the problem through repetitive testing. I believe my error came from the operator overloads to load my classes into the packet. Or it was my IP entry. One of the two, fixed it. Sadly I did not document the fix though I am sure that was one of the two issues.
Im trying to do multi client chat in c++ using TCP socket.
i have download the source of the socket implementation from this site.
The problem is when i try to send message to the server from the client,
the "ecko" that i recive from the server is endless string of spaces.
i tried to debug the client code, the client read the input properly.
In the first few message the server send back to the client his message,
but after few messages the client get back endless spaces.
i tried to use memset to nullify(put zero in all the array), but its making it worse, the server dont recive messages at all.
Would appreciate help (:
This is the server side:
#include "PracticalSocket.h"
#include <stdio.h>
#include <process.h>
#include <Windows.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
TCPSocket* MyClients[20];
int ClientCount = 0;
void connectCLient(void* pValue){
int nI,Flag;
char st[1024];
//memset(st,0,1024); // doing problems
TCPSocket* pServerClient = (TCPSocket*)pValue;
MyClients[ClientCount] = pServerClient;
ClientCount++;
try{
while (true)
{
Flag = pServerClient->recv(st,strlen(st));
if(Flag>1){
printf("%s\n",st);
for(nI = 0; nI< ClientCount ; nI++){
MyClients[nI]->send(st,strlen(st)+1);
}
}
}
}
catch(...){
puts("one client lefttt");
}
}
int main(int argc, char* argv[])
{
TCPServerSocket* pServer = new TCPServerSocket(8546);
int nClientCounter = 0;
printf("Start TCP Server ... on Port %d\n", 8546);
try{
while(true)
{
printf("Wait for new TCP Clients ... \n");
TCPSocket* pClient = pServer->accept();
_beginthread(connectCLient,0,(void*)pClient);
printf("Client %d Connected ... \n", ++nClientCounter);
}
}
catch(...){
puts("one client left");
}
return 0;
}
This is the Client side:
#include "PracticalSocket.h"
#include <stdio.h>
#include <process.h>
#include <Windows.h>
using namespace std;
#pragma comment (lib, "ws2_32.lib")
void ReciveMessages(void * pValue ){
char recvM[1024];
TCPSocket* pClient = (TCPSocket*)pValue;
while(true){
pClient->recv(recvM,strlen(recvM));
printf("%s\n",recvM);
}
}
int main(int argc, char* argv[])
{
try
{
TCPSocket * cClient = new TCPSocket();
cClient->connect("127.0.0.1",8546);
_beginthread(ReciveMessages,0,(void*)cClient);
char st[1024];
memset(st,0,1024);
while(true)
{
printf("Press Text -->");
fgets(st, sizeof st, stdin);
cClient->send(st,strlen(st)+2);
}
}
catch(...)
{
printf("Socket Error..!");
system("pause");//run cmd comment - stop the system
}
return 0;
}
There are a few mistakes in the code:
MyClients[ClientCount] = pServerClient;
ClientCount++;
Since the above happens in different threads, ClientCount++ is non-atomic and causes race conditions. Make ClientCount atomic or do that in one server thread.
In:
Flag = pServerClient->recv(st,strlen(st));
if(Flag>1) {
printf("%s\n",st);
for(nI = 0; nI< ClientCount ; nI++)
MyClients[nI]->send(st,strlen(st)+1);
st doesn't end with \0, because it can be a partial read, so that strlen(st) returns wrong results. Fix:
ssize_t received = pServerClient->recv(st, sizeof st - 1);
if(received > 0) {
st[received] = 0; // Zero-terminate.
printf("%s\n", st);
for(nI = 0; nI< ClientCount ; nI++)
MyClients[nI]->send(st, received);
Similar issue:
pClient->recv(recvM,strlen(recvM));
printf("%s\n",recvM);
Fix:
ssize_t received = pClient->recv(recvM, sizeof recvM - 1);
if(received > 0) {
recvM[received] = 0;
printf("%s\n",recvM);
}
And in:
cClient->send(st,strlen(st)+2);
No point in sending the zero terminator:
cClient->send(st, strlen(st));
TCP is a stream protocol which means that send and recv may send/receive partial data and there are no message boundaries. You may like to delimit your messages.
I am unsure how to solve my error. How do I make is so that my void run() function can see this variable 'intf'? the value of intf itself was declared separately in a .cnf file. Thank you
My errors are as follows
monreqserver.cc: In member function 'void Pds::MyXtcMonitorServer::run()':
monreqserver.cc:57: error: 'intf' was not declared in this scope
My code is as follows:
#include "pdsdata/app/XtcMonitorServer.hh"
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#define mult_address "225.0.0.37"
#define mult_port "1100"
namespace Pds {
class MyXtcMonitorServer : public XtcMonitorServer {
public:
MyXtcMonitorServer(const char* tag,
unsigned sizeofBuffers,
unsigned numberofEvBuffers,
unsigned numberofEvQueues, const char * intf) :
XtcMonitorServer(tag,
sizeofBuffers,
numberofEvBuffers,
numberofEvQueues)
{
_init();
}
~MyXtcMonitorServer() {}
public:
void run() {
//////////////
//udp socket//
//////////////
int udp_socket_info;
struct sockaddr_in udp_server;
udp_socket_info = socket(AF_INET, SOCK_DGRAM, 0);
if (udp_socket_info == -1) {
puts("Could not create socket");
}
udp_server.sin_addr.s_addr = inet_addr(mult_address);
udp_server.sin_port = htons(1100);
udp_server.sin_family = AF_INET;
ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, intf);
if (ioctl(udp_socket_info, SIOCGIFADDR, &ifr)<0) {
perror("SIOCGIFADDR failed");
}
char* port = "1100";
char* ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
char* zero = "/0";
char ip_port[100];
sprintf(ip_port, "%s %s %s", ip, port, zero);
sendto(udp_socket_info , ip_port , strlen(ip_port), 0, (struct sockaddr *)&udp_server, sizeof(udp_server));
puts("Message Sent");
///////////////////////////////
///SETTING UP TCP CONNECTION///
///////////////////////////////
int tcp_socket_info, tcp_new_socket;
struct sockaddr_in tcp_server, tcp_client;
tcp_socket_info = socket(AF_INET, SOCK_STREAM, 0);
if (tcp_socket_info == -1) {
printf("Could not create socket");
}
tcp_server.sin_addr.s_addr = INADDR_ANY;
tcp_server.sin_family = AF_INET;
tcp_server.sin_port = htons(1100);
int y=1;
if(setsockopt(tcp_socket_info, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) {
perror("set reuseaddr");
}
//binds socket
if (bind(tcp_socket_info, (struct sockaddr *)&tcp_server, sizeof(tcp_server)) < 0) {
perror("Bind error");
}
//listen
listen(tcp_socket_info , 5);
//waiting for connection
puts("Waiting for incoming connections...");
int c = sizeof(struct sockaddr_in);
//accept connection loop
tcp_new_socket = accept(tcp_socket_info, (struct sockaddr *)&tcp_client, (socklen_t*)&c);
puts("Connection accepted");
while(1)
sleep(1);
}
private:
void _copyDatagram(Dgram* dg, char*) {}
void _deleteDatagram(Dgram* dg) {}
void _requestDatagram() {}
private:
};
};
using namespace Pds;
void usage(char* progname) {
printf("Usage: %s -p <platform> -P <partition> -i <node mask> -n <numb shm buffers> -s <shm buffer size> [-q <# event queues>] [-t <tag name>] [-d] [-c] [-g <max groups>] [-h]\n", progname);
}
int main(int argc, char** argv) {
const unsigned NO_PLATFORM = unsigned(-1UL);
unsigned platform=NO_PLATFORM;
const char* partition = 0;
const char* tag = 0;
const char* intf = 0;
int numberOfBuffers = 0;
unsigned sizeOfBuffers = 0;
unsigned nevqueues = 1;
unsigned node = 0xffff;
unsigned nodes = 6;
bool ldist = false;
int c;
while ((c = getopt(argc, argv, "I:p:i:g:n:P:s:q:t:dch")) != -1) {
errno = 0;
char* endPtr;
switch (c) {
case 'p':
platform = strtoul(optarg, &endPtr, 0);
if (errno != 0 || endPtr == optarg) platform = NO_PLATFORM;
break;
case 'I':
intf = optarg;
break;
case 'i':
node = strtoul(optarg, &endPtr, 0);
break;
case 'g':
nodes = strtoul(optarg, &endPtr, 0);
break;
case 'n':
sscanf(optarg, "%d", &numberOfBuffers);
break;
case 'P':
partition = optarg;
break;
case 't':
tag = optarg;
break;
case 'q':
nevqueues = strtoul(optarg, NULL, 0);
break;
case 's':
sizeOfBuffers = (unsigned) strtoul(optarg, NULL, 0);
break;
case 'd':
ldist = true;
break;
case 'h':
// help
usage(argv[0]);
return 0;
break;
default:
printf("Unrecogized parameter\n");
usage(argv[0]);
break;
}
}
if (!numberOfBuffers || !sizeOfBuffers || platform == NO_PLATFORM || !partition || node == 0xffff) {
fprintf(stderr, "Missing parameters!\n");
usage(argv[0]);
return 1;
}
if (numberOfBuffers<8) numberOfBuffers=8;
if (!tag) tag=partition;
printf("\nPartition Tag:%s\n", tag);
MyXtcMonitorServer* apps = new MyXtcMonitorServer(tag,
sizeOfBuffers,
numberOfBuffers,
nevqueues, intf);
apps->distribute(ldist);
apps->run();
return 0;
}
and the header file include is as follows:
#ifndef Pds_XtcMonitorServer_hh
#define Pds_XtcMonitorServer_hh
#include "pdsdata/app/XtcMonitorMsg.hh"
#include "pdsdata/xtc/TransitionId.hh"
#include <pthread.h>
#include <mqueue.h>
#include <queue>
#include <stack>
#include <vector>
#include <poll.h>
#include <time.h>
namespace Pds {
class Dgram;
class TransitionCache;
class XtcMonitorServer {
public:
XtcMonitorServer(const char* tag,
unsigned sizeofBuffers,
unsigned numberofEvBuffers,
unsigned numberofEvQueues, const char * intf);
virtual ~XtcMonitorServer();
public:
enum Result { Handled, Deferred };
Result events (Dgram* dg);
void discover ();
void routine ();
void unlink ();
public:
void distribute (bool);
protected:
int _init ();
private:
void _initialize_client();
mqd_t _openQueue (const char* name, mq_attr&);
void _flushQueue (mqd_t q);
void _flushQueue (mqd_t q, char* m, unsigned sz);
void _moveQueue (mqd_t iq, mqd_t oq);
bool _send (Dgram*);
void _update (int,TransitionId::Value);
void _clearDest (mqd_t);
private:
virtual void _copyDatagram (Dgram* dg, char*);
virtual void _deleteDatagram(Dgram* dg);
virtual void _requestDatagram();
private:
const char* _tag; // name of the complete shared memory segment
unsigned _sizeOfBuffers; // size of each shared memory datagram buffer
unsigned _numberOfEvBuffers; // number of shared memory buffers for events
unsigned _numberOfEvQueues; // number of message queues for events
char* _myShm; // the pointer to start of shared memory
XtcMonitorMsg _myMsg; // template for messages
mqd_t _discoveryQueue; // message queue for clients to get
// the TCP port for initiating connections
mqd_t _myInputEvQueue; // message queue for returned events
mqd_t* _myOutputEvQueue; // message queues[nclients] for distributing events
std::vector<int> _myTrFd; // TCP sockets to clients for distributing
// transitions and detecting disconnects.
std::vector<int> _msgDest; // last client to which the buffer was sent
TransitionCache* _transitionCache;
int _initFd;
pollfd* _pfd; /* poll descriptors for:
** 0 new client connections
** 1 buffer returned from client
** 2 events to be distributed
** 3+ transition send/receive */
int _nfd;
mqd_t _shuffleQueue; // message queue for pre-distribution event processing
mqd_t _requestQueue; // message queue for buffers awaiting request completion
timespec _tmo;
pthread_t _discThread; // thread for receiving new client connections
pthread_t _taskThread; // thread for datagram distribution
unsigned _ievt; // event vector
};
};
#endif
The variable intf is a local variable of main(). It is therefore unknown outside the scope of main(). This is why you can't access it in the member functions of your class.
Three possible solutions:
you make the variable global (as it seems to be a global parameter that applies to all the classes
you make the variable a public static variable in the class. You then can initialize it from main, by using the prefix of your class.
or you define it as parameter of the run() member function (and invoke run from main accordingly).
The last one is the on which I'd choose, but i don't know enough about the context to give more objective advices for the choice:
// in the class:
void run(const char*intf) { // for convenience you can use the same name
...
}
// in main:
...
apps->run(intf); // pass the local variable as parameter
Name intf is referred in function run. But the compiler does not see any its declaration before its usage in the function
void run() {
//...
strcpy(ifr.ifr_name, intf);
^^^^^
I am building a client that:
Should be able to recieve information from both the server and the standart input
Should be able to recieve information from the server without asking, for example when another client sends a message.
To do so I tried using select to monitor both possible inputs.
What happens is that when a keyboard input is monitored I send a message to the client and I expect one back, so there's no problem. But when the server sends an unexpected message nothing happens, and I don't know why. Is using select() the proper way to do so? Is it even possible to use select() without listen()ing?
Here's my code (compileable):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <iostream>
#include <fstream>
#define MAX_CLIENT_NAME 30
#define MAX_TWIT_SIZE 140
#define NUM_OF_ARG 4
#define ERROR -1
#define GREAT_SUCCESS 0
#define OK "OK"
#define EXIT "EXIT"
using std::string;
using std::cerr;
using std::endl;
using std::cout;
string clientName;
int srverfd, numbytes, status, maxSock ;
fd_set inputFdSet; /* Socket file descriptors we want to wake
up for, using select() */
int establishConnection(char * serverAddress,char * port){
if ((srverfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
return ERROR;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
inet_aton(serverAddress, &server.sin_addr);
server.sin_port = htons(atoi(port));
memset(&(server.sin_zero), '\0', 8);
if (connect(srverfd,(const struct sockaddr *)&server,sizeof(struct sockaddr)) == -1) {
perror("connect");
close(srverfd);
return ERROR;
}
maxSock = srverfd;
return GREAT_SUCCESS;
}
const char * getUserTweet(){
string temp;
getline(std::cin,temp);
return temp.c_str();
}
void sendMessage(string message){
if ((numbytes = send(srverfd, message.c_str(), message.length(), 0)) == -1) {
perror("sendMessage");
close(srverfd);
}
cout<<"Message sent: "<< message << endl;
return;
}
const char * getMessage(){
char buf[MAX_TWIT_SIZE];
memset(buf,'\0',MAX_TWIT_SIZE);
if ((numbytes = recv(srverfd, buf, 140, 0)) == -1) {
perror("getMessage");
close(srverfd);
}
string temp = buf;
return temp.c_str();
}
void build_select_list() {
FD_ZERO(&inputFdSet);
FD_SET(srverfd,&inputFdSet);
FD_SET(STDIN_FILENO,&inputFdSet);
if (STDIN_FILENO > maxSock)
maxSock = STDIN_FILENO;
return;
}
void readSocket(fd_set tempfd) {
const char * tweet, * inMessage;
if (FD_ISSET(srverfd,&tempfd)) {
inMessage = getMessage();
cout << inMessage << endl;
}
if (FD_ISSET(STDIN_FILENO,&tempfd)) {
tweet = getUserTweet();
sendMessage(tweet);
inMessage = getMessage();
if (strcmp(inMessage,OK) != 0) {
cout << inMessage << endl;
}
if (strcmp(inMessage,EXIT) == 0) {
return;
}
}
return;
}
int main (int argc, char *argv[] ){
int value;
bool clientON = false;
if(establishConnection(argv[2],argv[3])){
cerr << "usage: failed to make connection" << endl << "exiting..." << endl;
exit(EXIT_FAILURE);
}
cout << "Connected successfully" << endl;
sendMessage("CONNECT "+clientName); //Connect
if(strcmp(getMessage(),OK) == 0){
clientON = true;
}
while(clientON){
build_select_list();
value = select(maxSock, &inputFdSet, NULL, NULL, NULL);
if (value < 0) {
perror("select");
exit(EXIT_FAILURE);
}
if (value == 0) {
continue;
}
else {
readSocket(inputFdSet);
}
}
sendMessage("DISCONNECT");
if(strcmp(getMessage(),OK) == 0){
// do nothing
}
close(srverfd);
return 0;
}
Your select call is invalid. The first parameter must be the highest file descriptor in any of the sets, plus one.
As you have it, an event on srverfd will not "wake up" the select call (unless STDIN_FILENO was somehow less than srverfd, in which case stdin events wouldn't unlock select - but that won't happen in practice).
There are quite a few other problems with your code. (It doesn't really look like C++.)
getUserTweet is unreliable (undefined behavior - temp is destroyed as soon as the function returns, so the char* you return has disappeared by the time its caller will try to use it). Same for getMessage. To remedy that, use std::string everywhere, and only extract the char* when you call into C library functions).
readSocket needlessly copies the FD set (can be expensive).
You should really get rid of all those globals - build one or two classes to encapsulate that state and the networking functions, or something like that.