//打开监听
private void button1_Click_1(object sender, EventArgs e)
{
HostIP = IPAddress.Parse(this.textBox1.Text.ToString());
point = new IPEndPoint(HostIP, Int32.Parse(this.textBox4.Text.ToString()));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Bind(point);
socket.Listen(3); //允许三人同时侦听 this.textBox2.Text += "侦听开启......"; acceptedSocket = socket.Accept();//当有可用的客户端连接时尝试执行,并返回一个新的socket,用于与客户端连接 Thread thread = new Thread(ToReceive);
thread.Start(); socket.Close();
thread.Abort();
acceptedSocket.Close();
//对接收到的数据进行解包
// UnPackeg_TLV(receiveByte, receiveByte.Length);
}
catch
{
}
// this.textBox2.Text += "侦听错误";
}
//去连接
private void ToReceive()
{
//如果连接上时
//接收消息
byte[] receiveByte = new byte[4112];
acceptedSocket.Receive(receiveByte,receiveByte.Length,0); this.textBox5.Text = Convert.ToString(receiveByte.Length); //用textbox显示,16进制显示
string strInfo = Encoding.ASCII.GetString(receiveByte);
this.textBox2.Text += "\r\n" + strInfo;
}
//关闭程序和socket
private void button3_Click(object sender, EventArgs e)
{
try
{
socket.Close();
acceptedSocket.Close();
this.Close();
}
catch
{
}
}
程序一般会在点击打开侦听按钮button1以后卡死,然后textbox2也不是每次很快的现实“开启侦听”的信息,客户端(这里是gprs做的客户端)发送数据过来也收不到数据,采用dehphi编写的socket接收程序能完整接收到数据。麻烦大家给点意见,再此拜谢!我实在找不出socket编写哪里有误。
private void button1_Click_1(object sender, EventArgs e)
{
HostIP = IPAddress.Parse(this.textBox1.Text.ToString());
point = new IPEndPoint(HostIP, Int32.Parse(this.textBox4.Text.ToString()));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Bind(point);
socket.Listen(3); //允许三人同时侦听 this.textBox2.Text += "侦听开启......"; acceptedSocket = socket.Accept();//当有可用的客户端连接时尝试执行,并返回一个新的socket,用于与客户端连接 Thread thread = new Thread(ToReceive);
thread.Start(); socket.Close();
thread.Abort();
acceptedSocket.Close();
//对接收到的数据进行解包
// UnPackeg_TLV(receiveByte, receiveByte.Length);
}
catch
{
}
// this.textBox2.Text += "侦听错误";
}
//去连接
private void ToReceive()
{
//如果连接上时
//接收消息
byte[] receiveByte = new byte[4112];
acceptedSocket.Receive(receiveByte,receiveByte.Length,0); this.textBox5.Text = Convert.ToString(receiveByte.Length); //用textbox显示,16进制显示
string strInfo = Encoding.ASCII.GetString(receiveByte);
this.textBox2.Text += "\r\n" + strInfo;
}
//关闭程序和socket
private void button3_Click(object sender, EventArgs e)
{
try
{
socket.Close();
acceptedSocket.Close();
this.Close();
}
catch
{
}
}
程序一般会在点击打开侦听按钮button1以后卡死,然后textbox2也不是每次很快的现实“开启侦听”的信息,客户端(这里是gprs做的客户端)发送数据过来也收不到数据,采用dehphi编写的socket接收程序能完整接收到数据。麻烦大家给点意见,再此拜谢!我实在找不出socket编写哪里有误。
private void button1_Click(object sender, System.EventArgs e)
{
//开放本地端口
myListener=new TcpListener(9927);
myListener.Start();
Thread thread=new Thread(new ThreadStart(GetConn));
thread.Name="MAIN";
thread.Start();
}
private void GetConn()
{
while(true)
{
this.remoteSocket=myListener.AcceptSocket();
richTextBox1.AppendText("\n有新加入的连接");
//获得新连接的反馈信息
string msg=ReceiveMsg(this.remoteSocket);
string[] newName=msg.Split('|');
if(newName[0]=="NAME")
{
User newUser=new User(newName[1],currentID.ToString(),remoteSocket);
currentID++;
//把用户信息保存到用户列表
allUsers.Add(newUser);
//刷新在线用户列表
MulSendUserName();
//发送用户id号
SendMsgById(newUser.Uid,"UID|"+newUser.Uid+"|");
//将新的用户列表保存早ListView里面(可选)
DisplayAllUser();
Thread thread=new Thread(new ThreadStart(GetMsg));
//为每一个获得信息的线程添加上名字
thread.Name=newUser.Uid;
thread.Start();
}
}
}