服务端与客户端
我写了这两个服务与客户端的程序,为什么老是出问题,有哪个高手帮忙解决一下服务端
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using .Sockets ;
using System.Threading;
namespace Receive__
{
public partial class Form1 : Form
{
private IPAddress myIP;
private IPEndPoint MyServer;
private Socket sock;
private bool bb;
private Socket aaa;
private Thread thread;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch { MessageBox.Show("您输入的IP地址不正确"); }
try
{
MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
textBox3.Text = "主机" + textBox1.Text + "端口" + textBox2.Text + "开始监听...";
thread = new Thread(new ThreadStart(targett));
thread.Start();
}
catch (Exception ee) { textBox3.Text = ee.Message; }
}
private void targett()
{
aaa = sock.Accept();
bb = false;
if (aaa.Connected)
{
textBox3.Text = "与客户建立连接。";
Byte[] bytee =new Byte[64];
bytee = System.Text.Encoding.BigEndianUnicode.GetBytes("您好,欢迎使用本服务器!".ToCharArray());
aaa.Send(bytee, bytee.Length, 0);
while (!bb)
{
Byte[] bbb = new Byte[64];
aaa.Receive(bbb, bbb.Length, 0);
string ccc = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
richTextBox1.AppendText(ccc + "\r\n");
int len = richTextBox1.Lines.Length;
if (richTextBox1.Lines[len - 2] == "@@@@@")
{
textBox3.Text = "与客户断开连接.";
aaa.Shutdown();
aaa.Close();
bb = true;
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
bb = true;
sock.Close();
textBox3.Text = "主机" + textBox1.Text + "端口" + textBox2.Text + "停止监听";
}
catch { MessageBox.Show("监听尚未开始,关闭无效"); }
}
}
}
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using
using System.Threading;
namespace 同步客户端
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse("127.0.0.1");
private IPEndPoint MyServer;
private Socket sock;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch { MessageBox.Show("输入的IP不正确"); }
try
{
MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(MyServer);
Thread thread=new Thread (new ThreadStart (targett));
thread.Start ();
textBox4.Text ="与主机"+textBox1.Text +"端口"+textBox2.Text +"连接成功!";
}
catch (Exception ee){MessageBox.Show (ee.Message );}
}
private void targett()
{
Byte[] bbb = new Byte[640];
sock.Receive(bbb, bbb.Length, 0);
string aaaaa = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
textBox4.Text = aaaaa;
}
private void button2_Click(object sender, EventArgs e)
{
Byte[] bytee = new Byte[640];
string send = textBox3.Text + "\r\n";
bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
sock.Send(bytee, bytee.Length, 0);
Thread thread=new Thread (new ThreadStart (targett));
thread.Start ();
}
private void button3_Click(object sender, EventArgs e)
{
Byte[] bytee = new Byte[64];
string send = "@@@@@" + "\r\n";
bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
sock.Send(bytee, bytee.Length, 0);
try
{
sock.Close();
textBox4.Text = "与主机 " + textBox1.Text + "端口" + textBox2.Text + "断开连接!";
}
catch { MessageBox.Show("连接尚未建立,断开无效"); }
}
}
}