0
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace PowerCarsMobileServer
{
    class Server
    {
        public static int Port { get; private set; }
        private static TcpListener tcpListener;
        public static ServerSocket serverSocket; //problem is here
        public static void Start(int _port)
        {
            Port = _port;
            serverSocket = new ServerSocket(Port);
            Console.WriteLine("Starting server...");
            InitializeServerData();
            tcpListener = new TcpListener(IPAddress.Any, Port);
            tcpListener.Start();
            Console.WriteLine($"Server started on {Port}.");
        }
    }
}

When creating a global port, I ran into the problem of not supporting "ServerSocket". Older articles use ServerSocket but I don't understand the problem comparing my code with theirs.

How can I fix it?

I tried to connect different references, but it doesn't work or I didn't find a good one

enter image description here

Aman B
  • 2,276
  • 1
  • 18
  • 26
  • 2
    Can you provide a link to the older article you are referring to? – Aman B Jan 22 '23 at 22:35
  • `ServerSocket` isn't a standard .NET BCL class. Gotta rewrite wherever you have it from. – Ray Jan 22 '23 at 22:36
  • How else can you solve the problem of server globalization? I need to make sure that the client can connect from different IP addresses, and not just localhost. https://stackoverflow.com/questions/33837325/android-serversocket-not-sending-data-to-client-client-c-server-java - the code presented here uses ServerSocket – Pavel Sheludko Jan 22 '23 at 22:47
  • 1
    @PavelSheludko: the link you refer to is Android/Java SDK and not C#/.NET. You have already solved your problem by listening on `IPAddress.Any`. – YK1 Jan 23 '23 at 00:32
  • @YK1: At the moment, a successful connection is carried out only using the local host (127.0.0.1). I think that you can solve the problem by opening the port I need, but I don’t know how to do it (Connection from the client side will be done from android and ios devices) – Pavel Sheludko Jan 23 '23 at 06:37
  • 1
    @PavelSheludko: likely because windows firewall may be blocking remote connections – YK1 Jan 23 '23 at 07:06

1 Answers1

1

You are using .Net and ServerSocket is an Android class.

To setup a server socket TCP connection in .Net:

    OptionalStateObject state = new OptionalStateObject();
    Socket newSocket = null;
    //A state object should be included and is required for all subsequent  
    //receives on the new socket. e.g. state.byteArray = new 
    //byte[BUFFER_SIZE];      
    Socket ServerTcpSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
    Task.Run(() => 
     {
         ServerTcpSocket.Listen(100);
         while(!cancel)
         {
             ServerTcpSocket.BeginAccept(new AsyncCallback(AcceptCallback),
                        state);
          }
     });

And in the AcceptCallback delegate:

    private void AcceptCallback(IAsyncResult ar)
    {
        try
        {
            newSocket = ServerTcpSocket.EndAccept(ar);
            state = (OptionalStateObject)ar.AsyncState;
        }
        catch(Exception ex)
        {   }
     }

And the connection is established on newSocket where you can start receiving:

    Task.Run(() => //replace with extension method
    {
        state.byteArray = new byte[BUFFER_SIZE];
        newSocket.BeginReceive(state.byteArray, 0,
             BUFFER_SIZE, SocketFlags.None, 
             new AsyncCallback(ReceiveCallback), state);
    });

The data can then be accessed from the state.byteArray in the ReceiveCallback delegate.

"How else can you solve the problem of server globalization? I need to make sure that the client can connect from different IP addresses, and not just localhost."

If the server app is listening in some remote location where the ip address is known (i.e. for all websites and server-side services on Azure or AWS) all external clients from any location on the internet will be able to initiate a connection for two-way communications.

gbRockland
  • 11
  • 1
  • 1
    Everything you said is generally correct, however it would be better not to access `ar` after calling `EndAccept`, since that function frees any resources held by `ar`. – Ben Voigt Jul 26 '23 at 19:37