当前位置:编程学习 > C#/ASP.NET >>

由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。(客户端代码)

//客户端代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace WinAp2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RichTextBox richTextBoxReceive;
private System.Windows.Forms.RichTextBox richTextBoxSend;
private System.Windows.Forms.ListBox listBoxState;
private System.Windows.Forms.Button buttonRequest;
private System.Windows.Forms.Button buttonSend;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.TextBox textBoxIP;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
//添加私有成员
private Socket socket;
private Thread thread;
private System.Windows.Forms.TextBox textBoxPort;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.richTextBoxSend.Text="";
this.richTextBoxReceive.Text="";
this.listBoxState.Items.Clear();

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
 //由于帖只太长该部分省略
}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
private void AcceptMessage()
{
while(true)
{
try
{
NetworkStream netStream=new NetworkStream(socket);
byte[] datasize=new byte[4];
netStream.Read(datasize,0,4);
int size=System.BitConverter.ToInt32(datasize,0);
byte[] message=new byte[size];
int dataleft=size;
int start=0;
while(dataleft>0)
{
int recv=netStream.Read(message,start,dataleft);
start+=recv;
dataleft-=recv;
}
this.richTextBoxReceive.Rtf=System.Text.Encoding.Unicode.GetString(message);
}
catch
{
this.listBoxState.Items.Add("与服务器断开");
break;
}
}
}

private void buttonRequest_Click(object sender, System.EventArgs e)
{
IPAddress ip=IPAddress.Parse(this.textBoxIP.Text);
    IPEndPoint server=new IPEndPoint(ip,Int32.Parse(this.textBoxPort.Text));
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
socket.Connect(server);
}
catch
{
MessageBox.Show("与服务器连接失败");
return;
}
this.buttonRequest.Enabled=false;
this.listBoxState.Items.Add("与服务器连接成功");
Thread thread=new Thread(new ThreadStart(AcceptMessage));
thread.Start();


}

private void buttonClose_Click(object sender, System.EventArgs e)
{
//try
//{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
this.listBoxState.Items.Add("与主机断开连接");
thread.Abort();
//}
/*catch
{
MessageBox.Show("尚未与主机连接,断开无效");
}
this.buttonRequest.Enabled=true;*/

}

private void buttonSend_Click(object sender, System.EventArgs e)
{
string str=this.richTextBoxSend.Rtf;
;
int i=str.Length;
if(i==0)
{
return;
}
else
{
i*=2;
}
byte[] datasize=new byte[4];
datasize=System.BitConverter.GetBytes(i);
byte[] sendbytes=System.Text.Encoding.Unicode.GetBytes(str);
try
{
NetworkStream netStream=new NetworkStream(socket);
netStream.Write(datasize,0,4);
netStream.Write(sendbytes,0,sendbytes.Length);
netStream.Flush();
this.richTextBoxSend.Text="";
}
catch
{
MessageBox.Show("无法连接");
}

}
private void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
try
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch
{
}
}


}
}
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,