SocketTCP - Chat qua lại giữa Client và Server.
Chạy trên cùng một máy nhé. Nếu 2 mấy tình chỉnh IPEnpoint lại.
Trên client gõ date để server trả về ngày tháng năm.
Gõ byby để thoát server và client
Server:
Mã:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SocketServer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("------------------------Server-----------------------------");
Console.WriteLine(" Chuong trinh chat 2 may voi nhau ");
Console.WriteLine(" Luu y: 1 ben goi va ben kia phai tra loi moi goi tiep duoc");
Console.WriteLine("-----------------------------------------------------------");
Console.WriteLine("---------------------Sunboy--2mit.org----------------------");
//tao soc ket
IPEndPoint ipserver = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5302);
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//bind
server.Bind(ipserver);
//listen
server.Listen(10);
Console.WriteLine("Chuan bi ket noi!");
Socket client = server.Accept();
Console.WriteLine("Chap nhan ket noi tu: {0}",client.RemoteEndPoint.ToString());
// string s;
byte[] data = new byte[1024];
string s = "Ket noi thanh cong den Server. Bat dau chat";
//Chuyen chuoi s thanh mang byte
//byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(s);
client.Send(data,data.Length,SocketFlags.None);
string input;
while (true)
{
data = new byte[1024];
int nhan = client.Receive(data);
s = Encoding.ASCII.GetString(data,0,nhan);
if (s.Equals("byby"))
break;
Console.WriteLine("Client: {0}",s);
input = Console.ReadLine();
data = new byte[1024];
data = Encoding.ASCII.GetBytes(input);
client.Send(data, data.Length, SocketFlags.None);
}
client.Shutdown(SocketShutdown.Both);
client.Close();
server.Close();
}
}
}
Client:
Mã:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SocketClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-------------------------Client------------------------");
Console.WriteLine(" Chuong trinh chat 2 may voi nhau ");
Console.WriteLine(" Luu y: 1 ben goi va ben kia phai tra loi moi goi tiep duoc");
Console.WriteLine("-----------------------------------------------------------");
Console.WriteLine("---------------------Sunboy--2mit.org----------------------");
//int a;
//int p;
IPEndPoint ipenpointclient = new IPEndPoint(IPAddress.Parse("127.0.0.1"),5302);
//IPEndPoint ipenpointclient = new IPEndPoint(a, 5302);
//Console.WriteLine("Moi nhap ip server: ");
//Console.ReadLine(IPAddress.Parse(a));
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipenpointclient);
byte[] data = new byte[1024];
int nhan = client.Receive(data);
// data = "Chao client";
string s = Encoding.ASCII.GetString(data,0,nhan);
Console.WriteLine(s);
string input;
string ngay;
while (true)
{
input = Console.ReadLine();
data = new byte[1024];
data = Encoding.ASCII.GetBytes(input);
client.Send(data,data.Length,SocketFlags.None);
data = new byte[1024];
nhan = client.Receive(data);
s = Encoding.ASCII.GetString(data, 0, nhan);
if (s.Equals("getdate"))
{
input = DateTime.Now.ToString("dd/MM/yyyy");
data = new byte[1024];
data = Encoding.ASCII.GetBytes(input);
client.Send(data, data.Length, SocketFlags.None);
} if (s.Equals("byby") || input.Equals("byby"))
break;
Console.WriteLine("Server: {0}",s);
}
client.Disconnect(true);
client.Close();
}
}
}