开发者

c# sockets help

I'm completely new to socket programming with C#, I'm trying to get two running .exes to talk to eachother:

static void Main(string[] args) {
    bool sender = !false;
    if (args.Length > 0) sender = !true;
    if (sender) {
        try {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8221);
            sock.Connect(ipe);
            while (true) {
            开发者_JAVA百科    string toSend = Console.ReadLine();
                sock.Send(Encoding.UTF32.GetBytes(toSend));
            }
        }
        catch (SocketException e) {
            Console.WriteLine(e.Message);
            Console.ReadLine();
        }
    }
    else {
        try {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 8221);
            sock.Bind(ipe);
            sock.Listen(4);
            while (true) {
                if (!sock.Connected) continue;
                byte[] buffer = new byte[1024];
                if (sock.Receive(buffer) > 0) Console.WriteLine(Encoding.UTF32.GetString(buffer));
            }
        }
        catch (SocketException e) {
            Console.WriteLine(e.Message);
            Console.ReadLine();
        }
    }
}

At the moment though, both programs run without error, but they don't seem to connect (if (!sock.Connected) always is true).

Please help, thank you.


Edit: Noticed that you don't have a sock.Accept() in your listener. You need to get a incoming socket that you can "talk on". Place a Socket c = sock.Accept() before your while(true) loop and use the c socket to send and receive data

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 8221);
sock.Bind(ipe);
sock.Listen(4);

Socket c = sock.Accept(); // added

while (true) {
    if (!c.Connected) continue;
    byte[] buffer = new byte[1024];
    if (c.Receive(buffer) > 0) Console.WriteLine(Encoding.UTF32.GetString(buffer));
}

And; As a recommendation, whenever I make socket programs, I usually send the number of bytes that I want to send before the actual buffer. If you are able to make your sender and listener connect this might be the fix to have them exchange data.

public void send(byte[] buf) {
    socket.Send(BitConverter.GetBytes(buf.Length), SocketFlags.None);
    socket.Send(buf, buf.Length, SocketFlags.None);
}

public byte[] receive() {
    byte[] lengthBytes = new byte[4];
    int read = socket.Receive(lengthBytes);
    // read contains the number of read bytes, so we can check it if we want
    int length = BitConverter.GetInt32(lengthBytes);
    byte[] buf = new byte[length];
    socket.Receive(buf);
    return buf;
}


No where in your server code you are accepting the incoming connections. You will have to accept and create a socket at the server end for the new incoming clients.

Socket newSock = sock.Accept();
if (!newSock.Connected) continue;
else
{...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜