2 Replies - 767 Views - Last Post: 02 May 2013 - 04:49 AM Rate Topic: -----

#1 Daaksin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 12

Sending a string over Python server to C# client problem

Posted 01 May 2013 - 12:38 AM

*Question originally asked by me on StackOverflow. However, I decided to ask it in an area with a very active Python community*

So I've been trying to create a simple chat program with python, but it's a combination of Python and C#... The server is built entirely out of Python and the client is C#. I use sockets for both of them. Data communication works 100%. When I click 'Connect' on the client, it connects and sends this message into the stream.

byte[] sendBytes = new byte[] { };
sendBytes = Encoding.ASCII.GetBytes("NEW_CONN|");
clientSock.Send(sendBytes);



The server receives this and passes it through to a "accepting" method. The reason I won't show this part is because it works 100%. The problem lies within Server to Client. I don't know if it's a cross-platform problem, but I sure as hell want to find out. Here is the Python server code (for the "parse" method - accept data from the client and respond):

def parseData(sock, message) :
splitString = message.split('|')

if splitString[0] == "NEW_CONN":
    sendString = "CONN_A|"
    sock.send(sendString)  
    CONNECTION_LIST.append(sock)
    print "Client connected: " + str(sock.getpeername())



Now, when the client receives this message, it is simply "7". In other words, the total number of chars that make up sendString. It isn't a client conversion issue, because I've tested outputting the string like this (serverside):

print str(sock.send(sendString))



That bit of code outputs "7" as well. What have I done wrong?
*OTHER INFO:
RECV_BUFFER = 4096 #Buffer amount variable

Receiving function (Python):
data = sock.recv(RECV_BUFFER)
parseData(sock, data)



Receiving function (C#):
const int bufferSize = 256;
byte[] buffer = new byte[bufferSize];

while (true)
{
    Parse(Convert.ToString(clientSock.Receive(buffer)));
}


Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Sending a string over Python server to C# client problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Sending a string over Python server to C# client problem

Posted 01 May 2013 - 06:56 AM

Quote

Now, when the client receives this message, it is simply "7". In other words, the total number of chars that make up sendString.

const int bufferSize = 256;
byte[] buffer = new byte[bufferSize];

while (true)
{
    Parse(Convert.ToString(clientSock.Receive(buffer)));
}




Pray tell, what does clientSock.Receive() return?

Quote

It isn't a client conversion issue, because I've tested outputting the string like this (serverside):

print str(sock.send(sendString))



That bit of code outputs "7" as well.


Again, what does sock.send() return?

If you don't understand these fairly primitive concepts -- or more importantly, how to discover the answers to those questions -- you're probably not quite ready to be writing chat servers/clients.
Was This Post Helpful? 0
  • +
  • -

#3 Daaksin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 12

Re: Sending a string over Python server to C# client problem

Posted 02 May 2013 - 04:49 AM

View PostJackOfAllTrades, on 01 May 2013 - 06:56 AM, said:

Quote

Now, when the client receives this message, it is simply "7". In other words, the total number of chars that make up sendString.

const int bufferSize = 256;
byte[] buffer = new byte[bufferSize];

while (true)
{
    Parse(Convert.ToString(clientSock.Receive(buffer)));
}




Pray tell, what does clientSock.Receive() return?

Quote

It isn't a client conversion issue, because I've tested outputting the string like this (serverside):

print str(sock.send(sendString))



That bit of code outputs "7" as well.


Again, what does sock.send() return?

If you don't understand these fairly primitive concepts -- or more importantly, how to discover the answers to those questions -- you're probably not quite ready to be writing chat servers/clients.


Please do not question my ability or understanding for sockets, you have no idea who I am. I got that working. It returns the string correctly. Only the first interaction, however. Everything sent after that is never put on a new line, it's not actually even parsed/understood. But the text reads fine. It has something to do with my Read method.

void Read()
        {
            try
            {
                    byte[] bytes = new byte[1024];
                    clientSock.Receive(bytes);
                    Parse(Encoding.ASCII.GetString(bytes));
                    Read();
            }
            catch
            {
                
            }
        }



I shall continue working on it by myself. I don't think I can really thank you for anything. But at least you answered, right?

P.S. Everyone needs answers to problems at some point. Isn't that why you're here? Or is it to be generally useless to every question asked?
Cheers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1