@koda wrote:
Hi,
I'm playing around with sending stuff with sockets and I'm encountering a problem with OF.
When calling receive(), receiveRaw or receiveRawBytes, the first call always returns empty. Am I doing something wrong?I started out doing a server and client in python to get the hang of sockets and now I'm moving the client over to OF. I've still got the server in python and run it on the same computer to keep things simple.
This is my "client" code (of):
void ofApp::keyReleased(int key){ //Open a a socket and try to receive some data cout << "ofApp::keyReleased(), Start receiving" << endl; ofxTCPClient client_socket; client_socket.setup(ADDRESS, PORT); if (client_socket.isConnected()) { const int bufsize = 100; //size of buffer to hold data char thedata[bufsize]; //buffer to hold received data // initialize buffer to all x's, to easily see if it's been overwritten for (int i = 0; i < bufsize; i++) { thedata[i] = 'x'; } //Print the buffer before receive cout << "thedata:"; cout << "\n------------------------------\n"; for (int i = 0; i < bufsize; i++) { cout << thedata[i]; } cout << "\n------------------------------\n"; // Read in data from the socket client_socket.receiveRawBytes(thedata, bufsize); //Print the buffer after receive cout << "thedata:"; cout << "\n------------------------------\n"; for (int i = 0; i < bufsize; i++) { cout << thedata[i]; } cout << "\n------------------------------\n"; // Cleanup cout << "Closing socket" << endl; client_socket.close(); } }
And this is my "Server" code (python):
### Send stuff like files for example ### import io import socket # Vars ADDRESS = 'localhost' SOCKET = 8001 # Start socket listening for connections print "Start Server" server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((ADDRESS, SOCKET)) server_socket.listen(0) print "Waiting for Connection" connection = server_socket.accept()[0] #returns a tupple with connection first print "Connection accepted" # Try to send stuff print "Start sending" theFile=open('test_send.jpg', 'rb') #theFile=open('test_send.txt', 'rb') print theFile MESSAGE = theFile.read() theFile.close() MESSAGE = "Hello world dfs!" #uncomment to just send a string sent = connection.send(MESSAGE) print "sent:" print sent print "Sending finished" # cleanup server_socket.close()
Posts: 1
Participants: 1