Thí Nghiệm Mạng Máy Tính LAB 2c: Nguyễn Văn Nam 1813168

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Nguyễn Văn Nam

1813168

THÍ NGHIỆM MẠNG MÁY TÍNH


LAB 2c
client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace client
{
class Program
{
static void Main(string[] args)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6969);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

try
{
client.Connect(ipep);

while (true)
{
byte[] datarecv = new byte[1024];
int recv = client.Receive(datarecv);
String s = Encoding.ASCII.GetString(datarecv, 0, recv);
Console.WriteLine("Client: {0}", s);
s = Console.ReadLine();
byte[] datasend = new byte[1024];
datasend = Encoding.ASCII.GetBytes(s);
client.Send(datasend, datasend.Length, SocketFlags.None);
}
client.Dispose();
}
catch(Exception e)
{
Console.WriteLine("Errors: {0}", e);
}
client.Close();
}
}
}
server.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows;

namespace server
{
class Program
{
static void Main(string[] args)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6969);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

server.Bind(ipep);
server.Listen(10);
Console.WriteLine("Waiting to connect.........");

Socket client=server.Accept();
Thread t = new Thread(() =>
{
while (true)
{
client = server.Accept();
Console.WriteLine("Accept to connect with: {0}",
client.RemoteEndPoint.ToString());

String s = "Welcom to server 6969";


byte[] datasend = new byte[1024];
datasend = Encoding.ASCII.GetBytes(s);
client.Send(datasend, datasend.Length, SocketFlags.None);
}
});
t.Start();
t.Join();

while (true)
{

}
client.Close();
server.Close();
}
}
}

You might also like