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

6
  • 2
    Can you provide a link to the older article you are referring to? Commented Jan 22, 2023 at 22:35
  • ServerSocket isn't a standard .NET BCL class. Gotta rewrite wherever you have it from. Commented Jan 22, 2023 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. stackoverflow.com/questions/33837325/… - the code presented here uses ServerSocket Commented Jan 22, 2023 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. Commented Jan 23, 2023 at 0:32
  • 1
    @PavelSheludko: likely because windows firewall may be blocking remote connections Commented Jan 23, 2023 at 7:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.