I am programming a server / client communication system where a client requests to log in to the server and can request to view other client's online status. I can make the client log in fine, but when I try to log in (successfully) and then send another packet requesting another client's information, the server does not receive that packet.
the main part of the server, not the technical connection stuff starting from bind Server:
Users client[2]; //I intialized them already
//Bind
bind(WelcomeSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));
//listening
listen(WelcomeSocket, 5);
//temp users
Users temp;
//while loop for the connection
while (1) {
ConnectionSocket = accept(WelcomeSocket, NULL, NULL);
if (recv(ConnectionSocket, RxBuffer, sizeof(RxBuffer), 0))
cout << "WORKEDDDDDDDD" << endl;
memcpy(&temp, RxBuffer, sizeof(struct Users));
cout << temp.message << temp.userName << endl << endl;
//check which message type is being sent
switch(temp.message) {
//if message type 1
case 1 :
for (int i = 0; i < 2; i++) {
//if receieved username matches with any username in the database
if (strcmp(temp.userName, client[i].userName) == 0) {
//assign the recieved users information to the matched one in database
strcpy(client[i].userName, temp.userName);
client[i].online = true;
client[i].message = 2;
cout << client[i].userName << endl << client[i].online << endl;
//send the acknowledgement packet
send(ConnectionSocket, (char *)&client[i], sizeof(struct Users), 0);
}
}
closesocket(ConnectionSocket);
break;
//if message type 3
case 3 :
cout << "3";
break;
default :
break;
}
}
closesocket(ConnectionSocket);
WSACleanup();
}
Client:
connect(ClientSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));
//cout << "Name: ";
//cin >> login;
//Send request to login
int log;
char * name = new char[128];
char * request = new char[128];
Users client;
Users talkto;
cout << "To login press (1). ";
cin >> log;
flushall();
if (log == 1) {
cout << "Username : ";
cin.getline(name, 128, '\n');
flushall();
//Set client login info
strcpy(client.userName, name);
client.message = 1;
send(ClientSocket, (char *)&client, sizeof(struct Users), 0);
//Recieve acknowledgement
recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);
//create temp users
Users temp;
memcpy(&temp, RxBuffer, sizeof(struct Users));
if (temp.message == 2) {
cout << "Enter user for user information: ";
cin.getline(talkto.userName, 128, '\n');
flushall();
talkto.message = 3;
//send request for user information packet
if (send(ClientSocket, (char *)&talkto, sizeof(struct Users), 0))
cout << "SENDT" << endl;
}
//cout << temp.userName << endl << temp.online << endl << temp.message;
closesocket(ClientSocket);
WSACleanup();
}
The structure for users
struct Users {
int message;
char userName[50];
char ipAddress[50];
int PortNumber;
bool online;
};
Not sure why it's not receiving information more than one time
ConnectionSocket = accept(WelcomeSocket, NULL, NULL);
You should put that before the loop, not inside, because the accept function is waiting for another client, blocking you from receiving another packet.
Also, when your recv call returns 0, it means that the connection has closed, and it's the end of the stream, so you should break from your loop when recv returns 0, or some unexpected SOCKET_ERROR.
For each client connection, your server performs accept-recv-send-closesocket. Period.
A connection to client is closed after the server processes the first packet. You have either to establish a new connection before sending each packet at client side (but that would probably make your login procedure useless) or to make server be able to maintain several concurrent client connections polling them in an infinite loop, maybe in a separate thread(s).
Related
sendmmsg/recvmmsg provide the option to send and receive multiple packets on socket in a single system call. Are these operations supported for TUN adaptor on Linux using C socket API.
Here is the sample code I tried but get errno=Socket operation on non-socket
struct mmsghdr hdrs[ARRAY_SIZE];
unsigned char data[ARRAY_SIZE][BUFF_SIZE];
struct iovec iovecs[ARRAY_SIZE];
memset(hdrs, 0, sizeof(hdrs));
for (int i = 0; i < ARRAY_SIZE; i++)
{
iovecs[i].iov_base = data[i];
iovecs[i].iov_len = BUFF_SIZE;
hdrs[i].msg_hdr.msg_iov = &iovecs[i];
hdrs[i].msg_hdr.msg_iovlen = 1;
}
while (true)
{
LOG_DEBUG(log__) << "blocking to read on fd=" << fd;
int retVal = recvmmsg(fd, hdrs, ARRAY_SIZE, 0, NULL);
LOG_DEBUG(log__) << "retVal=" << retVal;
if (retVal < 0)
{
LOG_ERROR(log__) << "failed in recvmmsg, retVal=" << retVal << ", errno=" << strerror(errno);
continue;
}
LOG_DEBUG(log__) << "read " << retVal << " messages";
for (int i = 0; i < retVal; i++)
{
LOG_DEBUG(log__) << "read data of length " << hdrs[i].msg_len;
}
}
I want to modify the dhcp4 source code to notify the ddns server to set the dynamic domain name when assigning the lease, but I added the nanomsg statement in dhcp4_srv.cc. When my nanomsg performs shutdown or close, the dhcp4 service will automatically close. This is why there is no other way to implement dynamic domain names (my dynamic domain name mainly sends the field set by the foreground and the mac address and IP address to the ddns server, or it may be the login account of the foreground).
Someone can help me? thank you very much.
if (lease) {
// We have a lease! Let's set it in the packet and send it back to
// the client.
// 我们有租约! 让我们在数据包中进行设置,然后将其发送回客户端。
if (fake_allocation) {
//租约建议
LOG_INFO(lease4_logger, DHCP4_LEASE_ADVERT)
.arg(query->getLabel())
.arg(lease->addr_.toText());
} else {
//成功授予租约
LOG_INFO(lease4_logger, DHCP4_LEASE_ALLOC)
.arg(query->getLabel())
.arg(lease->addr_.toText())
.arg(lease->valid_lft_);
int rc = 0;
int pair_socket = 0;
int str_len = 0;
char buf[256] = { 0 };
char buf1[256] = { 0 };
int timeo = 5000;
//计算长度
str_len = strlen(HELLOWORLD);
//初始化socket
pair_socket = nn_socket(1, NN_PAIR);
if (pair_socket == -1) {
printf("nn_socket failed! error: %s.\n", nn_err_strerror(errno));
//system("pause");
nn_err_abort();
//return 0;
}
//设置超时
rc = nn_setsockopt(pair_socket, 0, NN_SNDTIMEO, &timeo, sizeof(timeo));
rc = nn_setsockopt(pair_socket, 0, NN_RCVTIMEO, &timeo, sizeof(timeo));
//连接服务端
rc = nn_connect(pair_socket, SOCKET_ADDRESS2);
if (rc < 0) {
printf("bind failed! error: %s.\n", nn_err_strerror(errno));
//system("pause");
nn_err_abort();
//return 0;
}
//将hello world复制到buf中
memcpy(buf, HELLOWORLD, str_len);
//发送数据
rc = nn_send(pair_socket, buf, str_len, 0);
if (rc < 0) {
printf("nn_send failed! error: %s.rc = %d.\n", nn_err_strerror(errno), rc);
}
//打印
printf("send:%s\n", buf);
//这里主要是测试使用,平时项目不要使用标签
//接收数据
rc = nn_recv(pair_socket, buf1, 256, 0);
if (rc < 0) {
printf("nn_recv failed! error: %s.rc = %d.\n", nn_err_strerror(errno), rc);
}
//打印
printf("recv:%s\n", buf1);
memset(buf1, 0, 256);
//Sleep(1000);
//关闭套接字
rc = nn_shutdown(pair_socket, 1);
if (rc != 1) {
printf("nn_close failed! error: %s.\n", nn_err_strerror(errno));
//system("pause");
nn_err_abort();
//return 0;
}
std::cout << "testtttttttttttttttttttttttttttttttttt "
<< "hostnameeeeeeeeeeeeeeeeeeeeeeeeeeeeee:" << lease->hostname_ << std::endl;
std::cout << "testtttttttttttttttttttttttttttttttttt "
<< "ipppppppppppppppppppppppppppppppppppp:" << lease->addr_.toText() << std::endl;
std::cout << "lease ALLOC " << __LINE__ << " file name " << __FILE__
<< " HOST:" << lease->hostname_ << std::endl;
std::cout << "lease ALLOC " << __LINE__ << " file name " << __FILE__
<< " IP:" << lease->addr_.toText() << std::endl;
}
It sounds like you need some general information on the KEA software developed by Internet Systems Consortium aka: https://kea.isc.org/ I would read through the Kea docs, mailing lists and the general development wiki to try to hone in on the specific issue you are having with your code. Once you are able explain the issue you are having with your code you can edit your question with your details, and there is a much better chance that you will get more meaningful answers from this site.
I am building MQTT broker now.
SO, when I am receiving a MQTT packet from the client.That packet is also being send simultaneously.I can't find out the reason why. I am posting my code snippet below please do point out where am I going wrong .
static void onServEcho(struct bufferevent* bev, void* cbarg) {
EvBufferEvent evbuf(bev);
struct evbuffer *input = bufferevent_get_input(bev);
struct evbuffer *output = bufferevent_get_output(bev);
size_t len = evbuffer_get_length(input);
evutil_socket_t accept_fd = bufferevent_getfd(bev);
// Copy all the data from the input buffer to the output buffer.
char *data;
//data =(char *) malloc(len);
data = new char[len];
// cout<<"dasd"<<data<<endl;
evbuffer_copyout(input, data, len);
// evbuf.copy_data(input, data, len);
char converted[len];
int i;
for (i = 0; i < len; i++) {
sprintf(&converted[i * 2], "%02X", data[i]);
// cout<<"data is"<< data[i]<<endl;
*/
}
// std::stringstream stream;
// char packet[len];
//cout << "the hex data is" << converted << "with size" << len << endl;
// for(i=0;i<(2*len;i=i+2) {
// string connect_char;
// connect_char.insert(0,"0x");
/// connect_char.append(converted,i,2);
//char *buffer=static_cast<char*>(connect_char);
/// unsigned int buffer=strtoul(connect_char.c_str(), NULL, 16);
// char W = static_cast<char>(buffer);
// cout<<"the characterr is "<<W<<endl;
// }
//char *packet=convert(converted,20);
//cout<<"the packet converted is "<<packet<<endl;
string connect_comd;
//connect_comd="0x";
connect_comd.insert(0, "0x");
connect_comd.append(converted, 0, 2);
// strcpy(connect_comd,converted[0]);
// strcpy(connect_comd,converted[1]);
//unsigned int buf = strtoul(connect_comd.c_str(), NULL, 16);
//int buf=0x10;
int message_type = (ToBits(data[0]).to_ulong());
// cout<<"the message type received now is"<<GET_MESSAGE(data[0])<<endl;
//cout << "the connectcommand is" << buf << endl;
cout << "the message flag received now is" << ToBits(data[0]) << endl;
if (GET_MESSAGE(message_type) == CONNECT) {
cout << "connect packet received from mqtt client" << endl;
ConnectPack_Parser(data, len, accept_fd);
} else if (GET_MESSAGE(message_type) == SUBSCRIBE) {
cout << "subscribe packet received from mqtt client" << endl;
SubscribePack_Parser(data, len, accept_fd);
}
// }
// hexify()
//evbuffer_add_buffer(output, input);
evbuffer_add_buffer(output, input);
//evbuffer_remove_buffer(input,output,len);
free(data);
}
edit:
i am not just posting the code for a fix to the code but i am cluless as to how to avoid that send from the server end using lib event .
the line 'evbuffer_add_buffer(output, input);' automatically sends the received packet which i can trace from the wireshark.The thing is this line according to the libevent documentation is needed for optimal performance.
the code is working for now.I am posting it for those who may need it. If you only want the read operation don't use the ' evbuffer_add_buffer(output, input)' code since this instantly writes it to the socket or in other words sends the data to the connected client so the CONNECT packet was send simultaneously
I need help with organizing each request-handling for incoming connection in new thread (code is at the bottom of this topic).
I don't know at all how to organize manually ( without using boost/threadpool ) queue with handling each request? How should I solve such problem with non-using boost etc?
Cause, I want do it manually, and I don't understand how do the next:
Listening for each new connection
If I've got new connection, then send in new thread the handling
When thread ends handling process, close this thread
I have tried to do such stuff with while(true); but don't know how to organize well the request-queue to handle each HTTP-request.
My code is:
#include <iostream>
#include <Windows.h>
#pragma comment(lib, "Ws2_32.lib")
typedef struct Header
{
friend struct Net;
private:
WORD wsa_version;
WSAData wsa_data;
SOCKET sock;
SOCKADDR_IN service;
char *ip;
unsigned short port;
public:
Header(void)
{
wsa_version = 0x202;
ip = "0x7f.0.0.1";
port = 0x51;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(ip);
service.sin_port = htons(port);
}
} Header;
typedef struct Net
{
private:
int result;
HANDLE thrd;
DWORD exit_code;
void WSAInit(WSAData *data, WORD *wsa_version)
{
result = WSAStartup(*wsa_version, &(*data));
if(result != NO_ERROR)
{
std::cout << "WSAStartup() failed with the error: " << result << std::endl;
}
else
{
std::cout << (*data).szDescription << " " << (*data).szSystemStatus << std::endl;
}
}
void SocketInit(SOCKET *my_socket)
{
(*my_socket) = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if((*my_socket) == INVALID_SOCKET)
{
std::cout << "Socket initialization failed with the error: " << WSAGetLastError() << std::endl;
WSACleanup();
}
else
{
std::cout << "Socket initialization successful!" << std::endl;
}
}
void SocketBind(SOCKET *my_socket, SOCKADDR_IN *service)
{
result = bind((*my_socket), (SOCKADDR*)&(*service), sizeof(*service));
if(result == SOCKET_ERROR)
{
std::cout << "Socket binding failed with the error: " << WSAGetLastError() << std::endl;
closesocket((*my_socket));
WSACleanup();
}
else
{
std::cout << "Socket binding successful!" << std::endl;
}
result = listen(*my_socket, SOMAXCONN);
if(result == SOCKET_ERROR)
{
std::cout << "Socket listening failed with the error: " << WSAGetLastError() << std::endl;
}
else
{
std::cout << "Listening to the socket..." << std::endl;
}
}
void SocketAccept(SOCKET *my_socket)
{
SOCKET sock_accept = accept((*my_socket), 0, 0);
if(sock_accept == INVALID_SOCKET)
{
std::cout << "Accept failed with the error: " << WSAGetLastError() << std::endl;
closesocket(*my_socket);
WSACleanup();
}
else
{
std::cout << "Client socket connected!" << std::endl;
thrd = CreateThread(NULL, 0, &Net::Threading, &sock_accept, 0, NULL);
}
}
static void HandleRequest(char response[], int length)
{
std::cout << std::endl;
for(int i = 0; i < length; i++)
{
std::cout << response[i];
}
std::cout << std::endl;
}
static DWORD WINAPI Threading(LPVOID lpParam)
{
SOCKET *my_socket = (SOCKET*)lpParam;
char data[0x400];
int result = recv((*my_socket), data, sizeof(data), 0);
HandleRequest(data, result);
char *response = "HTTP/1.1 200 OK\r\nServer: Amegas.sys-IS/1.0\r\nContent-type: text/html\r\nSet-Cookie: ASD643DUQE7423HFDG; path=/\r\nCache-control: private\r\n\r\n<h1>Hello World!</h1>\r\n\r\n";
result = send((*my_socket), response, (int)strlen(response), 0);
if(result == SOCKET_ERROR)
{
std::cout << "Sending data via socket failed with the error: " << WSAGetLastError() << std::endl;
closesocket((*my_socket));
WSACleanup();
}
else
{
result = shutdown((*my_socket), 2);
}
return 0;
}
public:
Net(void)
{
Header *obj_h = new Header();
WSAInit(&obj_h->wsa_data, &obj_h->wsa_version);
SocketInit(&obj_h->sock);
SocketBind(&obj_h->sock, &obj_h->service);
SocketAccept(&obj_h->sock);
delete obj_h;
}
} Net;
int main(void)
{
Net *obj_net = new Net();
delete obj_net;
return 0;
}
Your OS will handle the accept() queueing - don't worry too much about it. Simple synchronous servers tend to run like this:
socket listeningSocket:=socket.create;
listeningSocket.bind('0.0.0.0',80); // address/port
listeningSocket.listen;
while(true){
socket serverClientSocket=accept(listeningSocket);
createThread(&serverClientThread,serverClientSocket);
}
void serverClientThread(void *param)
{
inBuffer char[256];
socket myServerClientSocket=(socket)param;
while(true){
int bytesRx=recv(myServerClientSocket,&inBuffer,size(inBuffer));
if (bytesRx>0){
if doSomethingWith(&inBuffer,bytesRx) // not necessarily size(inBuffer) bytes!!
{
send(myServerClientSocket,"Reply from server\r\n");
}
}
else
return; // on error or connection closed
}
}
The one listening thread, (can be main thread in console apps), runs the accept() loop foerver. The separate serverClientThread instances run until their client disconects or some other error occurs.
I am trying to have 2 way communication running between C sockets. For having the sockets set up, I was following the instructions on this link (http://www.linuxhowtos.org/C_C++/socket.htm) and everything worked fine. Sending messages from the client to the server worked perfectly.
However, I would also like the ability of the server to send response messages back to the client. How is this accomplished? If I set up a client and server connection at both ends, one of them cannot bind.
edit more code. Currently, I've used this style of sockets and put them into c++ code, just because that's my familiarity. Ignore the object oriented-ness.
//main call
if (server)
{
Connection recv(true, "servername");
recv.GetMsg();
recv.SendMsg("test", 4);
}
else // client
{
Connection snd(false, "servername");
snd.GetMsg();
snd.SendMsg("this is a test", 14);
}
And inside the Connection class,
void SendMsg(char *msg, int msg_length)
{
send(some_socket, msg, msg_length, 0);
};
void GetMsg()
{
recvd = recv(opponent_socket, buffer, sizeof(buffer), 0);
buffer[recvd] = '\0';
cout << "Printing msg: " << buffer << endl;
};
Connection::Connection(bool isserver, char *servername)
{
is_server = isserver;
server = servername;
opponent_socket = 0;
some_socket = socket(PF_INET, SOCK_STREAM, 0);
if (some_socket < 0)
{
cout << "Connection failed.\n" << endl;
exit(-1);
}
if (is_server)
{
AddrSetupServer(); // standard calls here. Pretty well what's shown in link provided
BindServer();
ListenServer();
WaitConnectionServer();
}
else
{
AddrSetupClient();
ConnectClient();
}
};
Once you have a connection, it is bidirectional; simply send your response over the socket.