CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

关于socket监听端口获取远程计算机发来信息的问题,我郁闷啊!请指教

楼主leonbingo(libin)2004-12-02 22:19:39 在 .NET技术 / C# 提问

请大家看下面的代码  
  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

相关问题

  • 如何使用SOCKET控件监听某一端口的UDP包
  • [求助]端口被监听
  • 不监听1433端口
  • 用vc写的服务端,打开了socket端口监听,如何在web页面中向端口发信息
  • 【菜鸟问题】服务器端保持监听端口(Socket)的问题!
  • 【菜鸟问题】服务器端同时监听多个端口(Socket)的问题!
  • 在一个socket守护服务进程中,如何实现同时监听两个端口?
  • 监听网络端口读异常
  • 关于端口的监听功能
  • 怎样对端口进行监听

关键词

  • .net
  • 代码
  • ip
  • null
  • label
  • button
  • borderstyle
  • drawing
  • textbox
  • disposing

得分解答快速导航

  • 帖主:leonbingo
  • popcorn

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo