Thí Nghiệm Mạng Máy Tính LAB 2c: Nguyễn Văn Nam 1813168
Thí Nghiệm Mạng Máy Tính LAB 2c: Nguyễn Văn Nam 1813168
Thí Nghiệm Mạng Máy Tính LAB 2c: Nguyễn Văn Nam 1813168
1813168
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());
while (true)
{
}
client.Close();
server.Close();
}
}
}