C# socket. Only able to receive first message

I am having some trouble figuring out why I am only receiving one reply from a server application running on my computer(LocalHost). I do not have the source for this server application but it is a java application. Messages that is sent is a xml structure and have to end with a EoT tag.

The communication:

  1. Client connect to sever.
  2. Client sends message to server.
  3. Server sends message recived to client.
  4. Client sends message to server.
  5. Server sends a End of Transmission character.
  6. Client sends message to server.
  7. Server sends a End of Transmission character.

This is how my client connect, send and receive:

public bool ConnectSocket(string server, int port)
System.Net.IPHostEntry hostEntry = null;

        // Get host related information.
        hostEntry = System.Net.Dns.GetHostEntry(server);
    catch (System.Exception ex)
            return false;

    // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
    // an exception that occurs when the host IP Address is not compatible with the address family
    // (typical in the IPv6 case).
    foreach (System.Net.IPAddress address in hostEntry.AddressList)
            System.Net.IPEndPoint ipe = new System.Net.IPEndPoint(address, port);
            System.Net.Sockets.Socket tempSocket = new System.Net.Sockets.Socket(ipe.AddressFamily, System.Net.Sockets.SocketType.Stream, 

            if (tempSocket.Connected)
                m_pSocket = tempSocket;
                m_pSocket.NoDelay = true;
                return true;
        return false;

public void Send(string message)
    message += (char)4;//We add end of transmission character

private void Recive()
    byte[] tByte = new byte[1024];
    string recivemessage = (m_Encoding.GetString(tByte));


Your Receive code looks very wrong; you should never assume that packets arrive in the same constructions that the server sends messages - TCP is just a stream. So: you must catch the return from Receive, to see how many bytes you received. It could be part of one message, an entire message, multiple entire messages, or the last half of one message and the first half of the next. Normally, you need some kind of "framing" decision, which could mean "messages split by LF characters", or could mean "the length of each message is prefixed by network-byte-order integer, 4 bytes". This usually means you need to buffer until you have a full frame, and worry about spare data at the end of the buffer that is part of the next frame. Key bits to add, though:

int bytes = m_pSocket.Receive(tByte);
// now process "bytes" bytes **only** from tByte, nothing that this
// could be part of a message, an entire message, several messages, or
// the end of message "A", the entire of message "B", and the first byte of
// message "C" (which might not be an entire character)

In particular, with text formats, you cannot start decoding until you are sure you have an entire message buffered, because a multi-byte character might be split between two messages.

There may well also be problems in your receive loop, but you don't show that (nothing calls Receive), so we can't comment.

Need Your Help

Customizing UIBarButtonItem

iphone objective-c uinavigationcontroller uinavigationbar uibarbuttonitem

I have a custom button I want to add onto the navigation bar. So here's what I have so far in my RootViewController (which inherits UIViewController, the UINavigationController is added through the

Trying to pass an array to a function and find the sum

c++ arrays

I am newbie to programming and I am trying to pass an array into a function and add all the elements together and return the sum. The problem is that I am getting a garbage value for the sum. I have