Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to connect client to server machine in c#
Posted
Comments
Uday P.Singh 13-Jun-11 13:20pm    
your question is vague, do you want something related to socket programming or developing web application want connection with database. Be more specific..
Sergey Alexandrovich Kryukov 14-Jun-11 1:24am    
Very much so...
--SA

//Server program

class SocketSerever
{
    public static void Main()
    {
        byte[] bytes = new byte[1024];
        IPHostEntry ipHost = Dns.Resolve("localhost");
        IPAddress ipAdress = ipHost.AddressList[0];
        IPEndPoint ipEndPoint = new IPEndPoint(ipAdress, 11111);
        Socket sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sListener.Bind(ipEndPoint);
            sListener.Listen(10);
            while (true)
            {
                Socket handler = sListener.Accept();
                handler.Receive(bytes);
                string receiveMessage = Encoding.ASCII.GetString(bytes);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
    }
}
//Client program
  
class ClientSocket
{
    public static void Main()
    {
        Serializations serialization = new Serializations();
        IPHostEntry ipHost = Dns.Resolve("127.0.0.1");
        IPAddress ipAddr = ipHost.AddressList[0];
        IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11111);
        Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sender.Connect(ipEndPoint);

        string message = "Helo server!";
        byte[] buffer = Encoding.ASCII.GetBytes(message);

        sender.Send(buffer);

        sender.Shutdown(SocketShutdown.Both);
        sender.Close();
    }
}
 
Share this answer
 
Sockets could be an option that you could use - Introduction to TCP client server in C#[^].
 
Share this answer
 
Networking can be used on different level I tried to describe in my past answers:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

—SA
 
Share this answer
 
also you can use an SQL server as the server, then the web friendly MSSQL connector functions to share data on many from it....fast low sync issues too
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900