Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

basic of c# sockets

// A C# program for Client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace Client {
 
class Program {
 
// Main Method
static void Main(string[] args)
{
    ExecuteClient();
}
 
// ExecuteClient() Method
static void ExecuteClient()
{
 
    try {
         
        // Establish the remote endpoint
        // for the socket. This example
        // uses port 11111 on the local
        // computer.
        IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddr = ipHost.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);
 
        // Creation TCP/IP Socket using
        // Socket Class Constructor
        Socket sender = new Socket(ipAddr.AddressFamily,
                   SocketType.Stream, ProtocolType.Tcp);
 
        try {
             
            // Connect Socket to the remote
            // endpoint using method Connect()
            sender.Connect(localEndPoint);
 
            // We print EndPoint information
            // that we are connected
            Console.WriteLine("Socket connected to -> {0} ",
                          sender.RemoteEndPoint.ToString());
 
            // Creation of message that
            // we will send to Server
            byte[] messageSent = Encoding.ASCII.GetBytes("Test Client<EOF>");
            int byteSent = sender.Send(messageSent);
 
            // Data buffer
            byte[] messageReceived = new byte[1024];
 
            // We receive the message using
            // the method Receive(). This
            // method returns number of bytes
            // received, that we'll use to
            // convert them to string
            int byteRecv = sender.Receive(messageReceived);
            Console.WriteLine("Message from Server -> {0}",
                  Encoding.ASCII.GetString(messageReceived,
                                             0, byteRecv));
 
            // Close Socket using
            // the method Close()
            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
        }
         
        // Manage of Socket's Exceptions
        catch (ArgumentNullException ane) {
             
            Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
        }
         
        catch (SocketException se) {
             
            Console.WriteLine("SocketException : {0}", se.ToString());
        }
         
        catch (Exception e) {
            Console.WriteLine("Unexpected exception : {0}", e.ToString());
        }
    }
     
    catch (Exception e) {
         
        Console.WriteLine(e.ToString());
    }
}
}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: methods c# 
Csharp :: how do you make a 2D object follow another 2D object in unity 2D 
Csharp :: c# struct 
Csharp :: remove multiple items from list c# 
Csharp :: c# convert list to array function 
Csharp :: is number c# 
Csharp :: c# csvhelper 
Csharp :: c# swap name in string 
Csharp :: c# convert enumb to int array 
Csharp :: what is a model in c# 
Csharp :: uri file path c# 
Csharp :: append 2 arrays c# 
Csharp :: winforms how to check for enter key 
Csharp :: C# bitwise operation 
Csharp :: multidimensional arrays c# 
Csharp :: all Substring of String 
Csharp :: TimeZone in asp.net core 
Csharp :: how to send button name for method in c# 
Csharp :: authentication and authorization in asp.net c# with example 
Csharp :: unity draw ray from one object to another 
Csharp :: c# string right extension 
Csharp :: c# catch multiple exception types 
Csharp :: c# wpf row definition height * in code 
Csharp :: Unity rainbow color changing object 
Csharp :: unity pause coroutine 
Csharp :: return stream from file c# 
Csharp :: How to search values in the registry 
Csharp :: wpf get dynamic resource from code 
Csharp :: c# only letters 
Csharp :: C# System.nanoTime 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =