[go: up one dir, main page]

0% found this document useful (0 votes)
27 views5 pages

Tugas2 Pemrogjar

Uploaded by

Ercken Djami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Tugas2 Pemrogjar

Uploaded by

Ercken Djami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

// 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 Costructor
        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 messagge 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 messagge 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());
    }
}
}
}

Bahsa Indonesia

/ Program C # untuk Klien

menggunakan Sistem;

menggunakan System.Net;

menggunakan System.Net.Sockets;
menggunakan System.Text;

Namespace Klien {

Program kelas {

// Metode Utama

static void Main (string [] args)

ExecuteClient ();

// Metode ExecuteClient ()

void statis ExecuteClient ()

coba {

// Buat titik akhir jarak jauh

// untuk soket. Contoh ini

// menggunakan port 11111 di lokal

// komputer.

IPHostEntry ipHost = Dns.GetHostEntry (Dns.GetHostName ());

IPAddress ipAddr = ipHost.AddressList [0];

IPEndPoint localEndPoint = IPEndPoint baru (ipAddr, 11111);

// Pembuatan Soket TCP / IP menggunakan

// Costructor Kelas Socket

Pengirim soket = Soket baru (ipAddr.AddressFamily,

SocketType.Stream, ProtocolType.Tcp);
coba {

// Hubungkan Socket ke remote

// titik akhir menggunakan metode Connect ()

sender.Connect (localEndPoint);

// Kami mencetak informasi Endpoint

// bahwa kita terhubung

Console.WriteLine ("Socket connected to -> {0}",

sender.RemoteEndPoint.ToString ());

// Pembuatan pesan itu

// kami akan kirim ke Server

byte [] messageSent = Encoding.ASCII.GetBytes ("Uji Klien <EOF>");

int byteSent = sender.Send (messageSent);

// Penyangga data

byte [] messageReceived = byte baru [1024];

// Kami menerima pesan menggunakan

// metode Receive (). Ini

// metode mengembalikan jumlah byte

// terima, itu akan kita gunakan untuk

// konversikan menjadi string

int byteRecv = sender.Receive (messageReceived);

Console.WriteLine ("Pesan dari Server -> {0}",

Encoding.ASCII.GetString (messageReceived,

0, byteRecv));

// Tutup Socket menggunakan

// metode Close ()
sender.Shutdown (SocketShutdown.Both);

sender.Close ();

// Kelola Pengecualian Socket

catch (ArgumentNullException ane) {

Console.WriteLine ("ArgumentNullException: {0}", ane.ToString ());

catch (SocketException se) {

Console.WriteLine ("SocketException: {0}", se.ToString ());

catch (Exception e) {

Console.WriteLine ("Pengecualian tak terduga: {0}", e.ToString ());

catch (Exception e) {

Console.WriteLine (e.ToString ());

You might also like