关于socket监听端口获取远程计算机发来信息的问题,我郁闷啊!请指教
请大家看下面的代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace test
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
Thread tr;
bool listen=true;
private System.Windows.Forms.TextBox IP;
private System.Windows.Forms.TextBox message;
private System.Windows.Forms.TextBox FPort;
private System.Windows.Forms.TextBox JPort;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
this.listen=false;
tr.Abort();
tr=null;
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.IP = new System.Windows.Forms.TextBox();
this.message = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.FPort = new System.Windows.Forms.TextBox();
this.JPort = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(12, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 12;
this.label1.Text = "对方ip";
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 80);
this.button2.Name = "button2";
this.button2.TabIndex = 10;
this.button2.Text = "侦听";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// IP
//
this.IP.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.IP.Location = new System.Drawing.Point(68, 24);
this.IP.Name = "IP";
this.IP.Size = new System.Drawing.Size(124, 21);
this.IP.TabIndex = 9;
this.IP.Text = "";
//
// message
//
this.message.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.message.Location = new System.Drawing.Point(68, 136);
this.message.Multiline = true;
this.message.Name = "message";
this.message.Size = new System.Drawing.Size(144, 80);
this.message.TabIndex = 6;
this.message.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 236);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "send";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 13;
this.label2.Text = "发送端口";
//
// FPort
//
this.FPort.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.FPort.Location = new System.Drawing.Point(68, 108);
this.FPort.Name = "FPort";
this.FPort.Size = new System.Drawing.Size(68, 21);
this.FPort.TabIndex = 7;
this.FPort.Text = "";
//
// JPort
//
this.JPort.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.JPort.Location = new System.Drawing.Point(68, 48);
this.JPort.Name = "JPort";
this.JPort.Size = new System.Drawing.Size(68, 21);
this.JPort.TabIndex = 8;
this.JPort.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 52);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 11;
this.label3.Text = "监听端口";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(240, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.IP);
this.Controls.Add(this.message);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.FPort);
this.Controls.Add(this.JPort);
this.Controls.Add(this.label3);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button2_Click(object sender, System.EventArgs e)
{
tr=new Thread(new ThreadStart(this.listening));
tr.Start();
}
private void listening()
{
try
{
TcpListener tcl=new TcpListener(int.Parse(this.JPort.Text));
tcl.Start();
Socket sok=tcl.AcceptSocket();
EndPoint ep=sok.RemoteEndPoint;
while(listen)
{
Byte[] cache=new byte[1024];
int i=sok.ReceiveFrom(cache,ref ep);
MessageBox.Show(System.Text.Encoding.UTF8.GetString(cache));
}
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
this.send();
}
private void send()
{
try
{
TcpClient tcc=new TcpClient(this.IP.Text,int.Parse(this.FPort.Text));
NetworkStream ns=tcc.GetStream();
StreamWriter st=new StreamWriter(ns);
st.Write(this.message.Text);
st.Flush();
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
}
}
}
现在的问题是我发送第一条消息的时候能接收到,可是第二条的时候就再没反应了,请大家帮助我啊,谢谢
问题点数:20、回复次数:1Top
1 楼popcorn(米花.珠海)回复于 2004-12-02 22:29:36 得分 20
将
Socket sok=tcl.AcceptSocket();
放到while里面,否则只能阻塞一次。Top




