Я пытаюсь отправить данные со стороны клиента (python) на серверную сторону, я могу отправить свои первые строковые данные и из второй строки, это не работает.
клиентская часть (Python)сторона):
# TCP Client Code
host="192.168.45.155" # Set the server address to variable host
port=8800 # Sets the variable port to 4446
from socket import * # Imports socket module
import json
s=socket(AF_INET, SOCK_STREAM) # Creates a socket
s.connect((host,port)) # Connect to server address
print "Successfully connected to the server and ready to send some data"
strike = json.dumps({"A":"STRIKE"})
s.send(strike)
strom = json.dumps({"A":"STROM"})
s.send(strom)
stab = json.dumps({"A":"STAB",B="1"})
s.send(stab)
msg=s.recv(1024) # Receives data upto 1024 bytes and stores in variables msg
print "Message from server : " + msg
#s.close() # Closes the socket
# End of code
Сторона сервера (C ++):
#include <jsoncpp/json/json.h>
using namespace std;
Json::Value root;
Json::Reader reader;
bool a,b,c;
string i;
//Server side
int main(int argc, char *argv[])
{
bool Style1;
//buffer to send and receive messages with
char msg[1500];
//setup a socket and connection tools
sockaddr_in servAddr;
memset((char*)&servAddr, 0,sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(8800);
//open stream oriented socket with internet address
//also keep track of the socket descriptor
int serverSd = socket(AF_INET, SOCK_STREAM, 0);
if(serverSd < 0)
{
cerr << "Error establishing the server socket" << endl;
exit(0);
}
//bind the socket to its local address
int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr,
sizeof(servAddr));
if(bindStatus < 0)
{
cerr << "Error binding socket to local address" << endl;
exit(0);
}
cout << "Waiting for a client to connect..." << endl;
//listen for up to 5 requests at a time
listen(serverSd, 5);
//receive a request from client using accept
//we need a new address to connect with the client
sockaddr_in newSockAddr;
socklen_t newSockAddrSize = sizeof(newSockAddr);
//accept, create a new socket descriptor to
//handle the new connection with client
int newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize);
if(newSd < 0)
{
cerr << "Error accepting request from client!" << endl;
exit(1);
}
cout << "Connected with client!" << endl;
//receive a message from the client (listen)
cout << "Awaiting client response..." << endl;
while(1){
memset(&msg, 0, sizeof(msg));//clear the buffer
recv(newSd, (char*)&msg, sizeof(msg), 0);
if(!strcmp(msg, "exit"))
{
cout << "Client has quit the session" << endl;
}
string str(msg);
cout << "Client: " << msg << endl;
cout << ">";
cout <<msg<<endl;
//Receiving the values and assigning to the variables
string text = msg;
bool parsingSuccessful = reader.parse( text, root );
if ( !parsingSuccessful )
{
cout << "Error parsing the string" << endl;
}
if(root["A"]=="STRIKE"){
printf("STRIKE CODE HERE \n\n\n\n\n\n");
}
else if(root["A"]=="STROM"){
printf("STROM CODE HERE \n\n\n\n\n\n");
}
else if(root["A"]=="STAB"){
printf("STAB CODE HERE \n\n\n\n\n\n");
i = root["B"].toStyledString();
string::size_type sz;
int a = stoi(i,&sz);
if(a==1){
printf("Almost over, STABBED ONCE \n");
}
}
else{
printf("Wrong data sent \n");
}
string data = "Instructions Received \n";
memset(&msg, 0, sizeof(msg)); //clear the buffer
strcpy(msg, data.c_str());
if(data == "exit")
{
//send to the client that server has closed the connection
send(newSd, (char*)&msg, strlen(msg), 0);
}
//send the message to client
send(newSd, (char*)&msg, strlen(msg), 0);
//we need to close the socket descriptors after we're all done
/* close(newSd);
close(serverSd);*/
}
cout << "Connection closed..." << endl;
return 0;
}
Что можно изменить, чтобы этот код работал и получал все строки?
Как можноЯ держу сервер открытым все время и отправляю сообщения от клиента, когда захочу, и как я могу хранить сообщения, отправленные клиентом, на сервере и выполнять их одно за другим?