0

i am trying to send and receive data via TcpSockets on my local machine.

I tried to use this tutorial: http://www.csharp-examples.net/socket-send-receive/

Therefore i have two functions "Receive" and "Send":

My code:

public static void Main()
{
    Socket socket = new TcpClient("Ultimate-PC", 8080).Client;
    byte[] buffer = new byte[12];
    string txt = "Hello";
    try
    {
        Send(socket,Encoding.UTF8.GetBytes(txt), 0, Encoding.UTF8.GetBytes(txt).Length,10000);
        Receive(socket, buffer, 0, buffer.Length, 10000);
        string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
        Console.WriteLine(str);
    }
    catch (Exception)
    {

        throw;
    }
}

public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
    int startTickCount = Environment.TickCount;
    int sent = 0;  // how many bytes is already sent
    do
    {
        if (Environment.TickCount > startTickCount + timeout)
            throw new Exception("Timeout.");
        try
        {
            sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
        }
        catch (SocketException ex)
        {
            if (ex.SocketErrorCode == SocketError.WouldBlock ||
                ex.SocketErrorCode == SocketError.IOPending ||
                ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
            {
                Thread.Sleep(30);
            }
            else
                throw ex;
        }
    } while (sent < size);
}

public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
    int startTickCount = Environment.TickCount;
    int received = 0;
    do
    {
        if (Environment.TickCount > startTickCount + timeout)
            throw new Exception("Timeout.");
        try
        {
            received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
        }
        catch (SocketException ex)
        {
            if (ex.SocketErrorCode == SocketError.WouldBlock ||
                ex.SocketErrorCode == SocketError.IOPending ||
                ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
            {
                Thread.Sleep(30);
            }
            else
                throw ex;
        }
    } while (received < size);
}

The problem is, that after the socket initialization nothing happens in debug mode. Any ideas how i could get it to work?

binaryBigInt
  • 1,526
  • 2
  • 18
  • 44
  • Where did all the comments go? – binaryBigInt Aug 04 '15 at 20:27
  • 1
    They were under *Philip Stuyck*'s answer, and he deleted his answer. – EZI Aug 04 '15 at 21:01
  • 1
    http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C is what you need. Create a console application for the client and one for the server. Run them on the same machine, and use loopback. – Philip Stuyck Aug 05 '15 at 06:11
  • Thanks man! Finally it works. For anyone who faces the same problem, just split the two classes from here: http://pastebin.com/N8XUvAd9 – binaryBigInt Aug 05 '15 at 08:17
  • 1
    I kept being downvoted while my answer was perfectly reasonable. Some downvoters should read the comments as well. I think I understood what the problem was and gave the right advice. – Philip Stuyck Aug 05 '15 at 11:44

0 Answers0