0

I have the client side in C# running from Unity and it's just this

byte[] buffer = { 1 };
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(IP, PORT);
System.Int32 Result = s.Send(buffer);
Debug.Log(Result);

and the server side is written in C, it's really long so here is the place where I get the error

for(char i = 0; i < MAX_PLAYERS; ++i)
{
            if(ClientSockets[i] == INVALID_SOCKET) break;
            

            Result = recv(ClientSockets[i], Message, sizeof(Message), 0);
            
            if (Result == SOCKET_ERROR)
            {
                unsigned long LastError = GetLastError();
                if (LastError != 10035)
                {
                    Error("recv failed.\n");
                }
            }

I've made the socket's non blocking with ioctlsocket(ClientSocket, FIONBIO, &iMode) before putting them in the ClientSockets array.

  • What is IP and PORT? There is a few reasons why you get this error message. One common reason is if the IP address is the loopback 127.0.0.1 and you use Send() method. Why your client and server are on the same machine you have to make sure the listener and sender do not use the same IP. So you use for listener IP.Any and client you use IP address of the machine. – jdweng Feb 23 '21 at 12:55
  • @jdweng the ip is "192.168.1.100" and the port is 7042 – badProgrammer0123456 Feb 23 '21 at 12:57
  • Does this answer your question: [C socket: recv and send all data](https://stackoverflow.com/questions/13479760/c-socket-recv-and-send-all-data) ? -> Accepted answer states: `The recv() and send() functions do not guarantee to send/recv all data (see man recv, man send)` – derHugo Feb 23 '21 at 13:07
  • Check from cmd.exe>Netstat -a and see if port is already used. The listener must be started before client. If on same machine the listener cannot use IP 192.168.1.100. – jdweng Feb 23 '21 at 13:17

1 Answers1

0

Fixed it. My problem was that the socket on the client side was created inside a function so it disappeared after the function finished executing.