Laporan Praktikum 3 PJ

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

LAPORAN PRAKTIKUM 3

UDP Client & Server

PEMROGRAMAN JARINGAN

DISUSUN OLEH

RIYO WISANG W (092410101020)

Program Studi Sistem Informasi

UNIVERSITAS JEMBER

2011
A. PROGRAM PERCOBAAN

CLIENT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net; // For IPEndPoint
using System.Net.Sockets; // For UdpClient, SocketException

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void textBox2_TextChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{

String server = textBox1.Text; // Server name or IP address

// Use port argument if supplied, otherwise default to 7


int servPort = (richTextBox1.Text.Length == 3) ?
Int32.Parse(richTextBox1.Text) : 7;

// Convert input String to an array of bytes


byte[] sendPacket = Encoding.ASCII.GetBytes(textBox1.Text);

// Create a UdpClient instance


UdpClient client = new UdpClient();
try
{
// Send the echo string to the specified host and port
client.Send(sendPacket, sendPacket.Length, server, servPort);

richTextBox1.Text += "Sent " + sendPacket.Length + " bytes to the


server...";

// This IPEndPoint instance will be populated with the remote


sender’s
// endpoint information after the Receive() call
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);

// Attempt echo reply receive


byte[] rcvPacket = client.Receive(ref remoteIPEndPoint);

richTextBox1.Text += "\n\nReceived " + rcvPacket.Length+ " bytes


from " +remoteIPEndPoint+ " : " +
Encoding.ASCII.GetString(rcvPacket, 0, rcvPacket.Length);
}
catch (SocketException se)
{
richTextBox1.Text += se.ErrorCode + " : " + se.Message;
}
}

private void richTextBox1_TextChanged(object sender, EventArgs e)


{

private void Form1_Load(object sender, EventArgs e)


{

}
}
}

SERVER
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace UdpEchoServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

void server()
{
int servPort = 7;
UdpClient client = null;

try
{
client = new UdpClient(servPort);
}
catch (SocketException se)
{
MessageBox.Show(se.ErrorCode + ": " + se.Message);
Environment.Exit(se.ErrorCode);
}
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);

for (; ; )
{
try
{
byte[] byteBuffer = client.Receive(ref remoteIPEndPoint);

client.Send(byteBuffer, byteBuffer.Length, remoteIPEndPoint);


MessageBox.Show("handling client at " + remoteIPEndPoint + " -
" + Environment.NewLine + "echoed" + byteBuffer.Length + " bytes.");

}
catch (SocketException se)
{
MessageBox.Show(se.ErrorCode + ": " + se.Message);

}
}
}
private void button1_Click(object sender, EventArgs e)
{

Thread server1 = new Thread(new ThreadStart(server));


server1.IsBackground=true;
server1.Start();

private void Form1_Load(object sender, EventArgs e)


{

}
}
}
A. DATA HASIL PERCOBAAN

1. Sebelum connect dengan IP Default, aktifkan server.

Dengan mengklik Button Activated

2. Jalankan Client dengan menginput “Test Praktikum 3 UDP” pada IP Address 127.0.0.1
3. Hasil Setelah klik Send

You might also like