Các bạn ơi giúp mình với! Mình viết 1 chương trình client/sever. Mình dùng đến hàm thread.sleep() trong vòng while khi mà mạng còn kết nối thì thực hiện gửi nhận dữ liệu giữa hai máy. Vấn đề là khi đang chạy thì không sao nếu như mình tắt đột xuất một trong hai chương trình thì báo lỗi. Đây là code cúa mình mong các bạn gỡ rối giúp mình với. Cảm ơn nhiều.
Chương trình server:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;
namespace QuanSatServerNew
{
class KetNoi
{
public Socket socket;
public Socket client;
byte[] strnhan;
public void MoKetNoi()
{
IPEndPoint IP_End = new IPEndPoint(IPAddress.Any, 5000);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//giu socket tai dia chi dich
socket.Bind(IP_End);
//dat socket trong trang thai lang nghe
socket.Listen(10);
//chap nhan luong ket noi tu socket
client = socket.Accept();
IPEndPoint ttclient = (IPEndPoint)client.RemoteEndPoint;
MessageBox.Show(Convert.ToString(ttclient.Address) + " , " + Convert.ToString(ttclient.Port));
byte[] data = Encoding.ASCII.GetBytes("Ket noi thanh cong !");
client.Send(data, data.Length, SocketFlags.None);
while(true)
{
if (!client.Connected)
break;
else
{
strnhan = new byte[1024];
int nhan = client.Receive(strnhan, 0, strnhan.Length, SocketFlags.None);
MessageBox.Show(Encoding.ASCII.GetString(strnhan, 0, nhan));
data = Encoding.ASCII.GetBytes("Ket noi thanh c !");
client.Send(data, data.Length, SocketFlags.None);
}
Thread.Sleep(2000);
}
client.Close();
socket.Close();
}
public void GuiDuLieu(string s)
{
byte[] data = Encoding.ASCII.GetBytes(s);
client.Send(data, data.Length, SocketFlags.None);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace QuanSatServerNew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
KetNoi kn = new KetNoi();
private void Form1_Load(object sender, EventArgs e)
{
kn.MoKetNoi();
}
private void Form1_Close(object sender, EventArgs e)
{
MessageBox.Show("Form closing");
kn.GuiDuLieu("exit");
}
}
}
Chương trình client:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;
namespace QuanSatClientNew
{
class KetNoi
{
Socket skketnoi;
public void moKetNoi(string dc)
{
skketnoi = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int port = 5000;
IPEndPoint ip_end = new IPEndPoint(IPAddress.Parse(dc), port);
try
{
skketnoi.Connect(ip_end);
}
catch (SocketException)
{
MessageBox.Show("Ket noi that bai!");
}
if (skketnoi.Connected)
{
byte[] strnhan = new byte[1024];
int nhan = skketnoi.Receive(strnhan, 0, strnhan.Length, SocketFlags.None);
if (string.Compare(Encoding.ASCII.GetString(strnhan, 0, nhan), "exit") == 0)
skketnoi.Close();
MessageBox.Show(Encoding.ASCII.GetString(strnhan, 0, nhan));
while (true)
{
if (skketnoi.Connected)
{
byte[] data = Encoding.ASCII.GetBytes("Da nhan dl!");
skketnoi.Send(data, data.Length, SocketFlags.None);
}
if (skketnoi.Connected)
{
nhan = skketnoi.Receive(strnhan, 0, strnhan.Length, SocketFlags.None);
if (string.Compare(Encoding.ASCII.GetString(strnhan, 0, nhan), "exit") == 0)
skketnoi.Close();
MessageBox.Show(Encoding.ASCII.GetString(strnhan, 0, nhan));
}
Thread.Sleep(2000);
}
}
skketnoi.Close();
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace QuanSatClientNew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
KetNoi kn = new KetNoi();
kn.moKetNoi("127.0.0.1");
}
}
}