S
S
Stepan Gervik2019-01-07 11:49:55
C++ / C#
Stepan Gervik, 2019-01-07 11:49:55

How to setup C# sockets?

Hello! Such a problem has arisen. It is necessary that the client and the server communicate with each other, i.e. The client could listen and send data, and the server had the same characteristics.
Here is the server

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        public void getText ()
        {
            socket.Bind(new IPEndPoint(IPAddress.Any, 80));
            socket.Listen(1000000);
                Socket client = socket.Accept();
                byte[] buffer = new byte[1024];
                client.Receive(buffer);
                Console.WriteLine(Encoding.ASCII.GetString(buffer));
                Console.ReadKey();
        }

And here is the client
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        public void connect()
        {

            socket.Connect("127.0.0.1", 80);
        }
        public void GiveOnline (string message)
        {
            connect();
            byte[] buffer = Encoding.ASCII.GetBytes(message);
            socket.Send(buffer);
            socket.Bind(new IPEndPoint(IPAddress.Any, 8080));
            socket.Listen(1);
            socket.Accept();
            socket.Receive(buffer);

        }

The problem is that when the client starts, it complains about the line with IPEndPoint: An unhandled
exception of type "System.Net.Sockets.SocketException" in System.dll
An invalid argument

was received Ports are different for users (I read on the forum that this is a problem, but nothing helped)

System.Net.Sockets.SocketException
HResult=0x80004005
Message = Invalid Argument Received
Source = System
Stack Trace:
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket .Bind(EndPoint localEP)
in Client.Give.GiveOnline(String message) in C:\Users\Home\source\repos\Client\Client\Give.cs: line 24
in Client.Program.Main(String[] args) in C:\Users\Home\source\repos\Client\Client\Program.cs: line 14

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shai_hulud, 2019-01-07
@HiLevel

https://stackoverflow.com/questions/1777629/how-to...
> You cannot bind a single socket to multiple endpoints. A SocketException (invalid argument error) occurs the second time you call Bind() for a given socket.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question