Search code examples
c#serversystem.netsystem.net.sockets

How to create new ServerSocket C#


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


Solution

  • 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.