C# Can';当服务器在聊天套接字Winform应用程序中检索客户端时,无法接收数据
我正在Windows窗体应用程序中实现一个聊天套接字程序。我的客户端程序无法从服务器接收数据。我不知道我的问题到底是什么,所以我无法描述更多的细节。我是socket编程的新手,我的客户端程序很糟糕。我怎样才能改进这个程序 Client.csC# Can';当服务器在聊天套接字Winform应用程序中检索客户端时,无法接收数据,c#,winforms,sockets,C#,Winforms,Sockets,我正在Windows窗体应用程序中实现一个聊天套接字程序。我的客户端程序无法从服务器接收数据。我不知道我的问题到底是什么,所以我无法描述更多的细节。我是socket编程的新手,我的客户端程序很糟糕。我怎样才能改进这个程序 Client.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syste
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace ChatClient
{
public partial class Form1 : Form
{
byte[] data = new byte[1024];
string input;
int port;
TcpClient server;
int recv;
public event FormClosedEventHandler FormClosed;
string stringData;
public Form1()
{
InitializeComponent();
Form_Load();
}
/// <summary>
///
/// </summary>
public void Form_Load()
{
rtbContent.AppendText("Please Enter the port number of Server:\n");
}
/// <summary>
/// Function connect to server with port number
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
input = txtPort.Text;
port = Int32.Parse(input);
try
{
server = new TcpClient("127.0.0.1", port);
}
catch (SocketException)
{
rtbContent.AppendText("Unable to connect to server");
return;
}
rtbContent.AppendText("Connected to Server...\n");
}
/// <summary>
/// Function send data to server.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
NetworkStream ns = server.GetStream();
StateObject state = new StateObject();
state.workSocket = server.Client;
server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback((new Form1()).OnReceive), state);
input = rtbSend.Text;
if (input == "exit")
return;
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
ns.Flush();
rtbContent.AppendText(rtbSend.Text);
rtbSend.Text = "";
receiveData(sender, e);
ns.Close();
}
/// <summary>
/// Event called when server send data for client.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void receiveData(object sender, EventArgs e)
{
NetworkStream ns = server.GetStream();
StateObject state = new StateObject();
server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback((new Form1()).OnReceive), state);
data = new byte[1024];
recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
rtbContent.AppendText(stringData);
}
/// <summary>
/// Event called when Form close.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosed(Object sender, FormClosedEventArgs e)
{
rtbContent.AppendText("Disconnecting from server...");
server.Close();
MessageBox.Show( "FormClosed Event");
}
/// <summary>
/// Asynchronous Callback function which receives data from client
/// </summary>
/// <param name="ar"></param>
public void OnReceive(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead;
if (handler.Connected)
{
// Read data from the client socket.
try
{
bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Remove(0, state.sb.Length);
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Display Text in Rich Text Box
content = state.sb.ToString();
rtbContent.AppendText(content.ToString());
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(OnReceive), state);
}
}
catch (SocketException socketException)
{
//WSAECONNRESET, the other side closed impolitely
if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
{
handler.Close();
}
}
// Eat up exception....
catch (Exception exception)
{
MessageBox.Show(exception.Message + "\n" + exception.StackTrace);
}
}
}
}
/// <summary>
/// Server receive state
/// </summary>
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace ChatServer
{
public partial class ChatDialog : Form
{
private TcpClient client;
private NetworkStream clientStream;
public delegate void SetTextCallback(string s);
private Form1 owner;
public TcpClient connectedClient
{
get { return client; }
set { client = value; }
}
#region Constructors
public ChatDialog()
{
InitializeComponent();
}
/// <summary>
/// Constructor which accepts Client TCP object
/// </summary>
/// <param name="parent"></param>
/// <param name="tcpClient"></param>
public ChatDialog(Form1 parent, TcpClient tcpClient)
{
InitializeComponent();
this.owner = parent;
// Get Stream Object
connectedClient = tcpClient;
clientStream = tcpClient.GetStream();
// Create the state object.
StateObject state = new StateObject();
state.workSocket = connectedClient.Client;
// Call Asynchronous Receive Function
connectedClient.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(OnReceive), state);
//connectedClient.Client.BeginDisconnect(true, new AsyncCallBack(DisconnectCallBack),state);
rtbChat.AppendText("Chat here");
}
#endregion
/// <summary>
/// This function is used to display data in Rick Text Box
/// </summary>
/// <param name="text"></param>
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.rtbChat.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.rtbChat.SelectionColor = Color.Blue;
this.rtbChat.SelectedText = "\nFriend: " + text;
}
}
#region Send/Receive Data From Sockets
/// <summary>
/// Function to Send Data to Client
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
byte[] bt;
bt = Encoding.ASCII.GetBytes(txtMessage.Text);
connectedClient.Client.Send(bt);
rtbChat.SelectionColor = Color.IndianRed;
rtbChat.SelectedText = "\nMe: " + txtMessage.Text;
txtMessage.Text = "";
}
/// <summary>
/// Asynchronous Callback function which receives data from server
/// </summary>
/// <param name="ar"></param>
private void OnReceive(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// form the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead;
if (handler.Connected)
{
// Read data from the client socket.
try
{
bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Remove(0, state.sb.Length);
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Display Text in Rich Text Box
content = state.sb.ToString();
SetText(content);
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(OnReceive), state);
}
}
catch (SocketException socketException)
{
//WSAECONNRESET, the other side closed impolitely
if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
{
// Complete the disconnect request.
String remoteIP = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
String remotePort = ((IPEndPoint)handler.RemoteEndPoint).Port.ToString();
this.owner.DisconnectClient(remoteIP, remotePort);
handler.Close();
handler = null;
}
}
// Eat up exception ...
catch (Exception exception)
{
MessageBox.Show(exception.Message + "\n" + exception.StackTrace);
}
}
}
#endregion
#region Commands
/// <summary>
/// Exit Event Handler. Exit menu item is selected, the dialog box is hidden from user.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
}
/// <summary>
/// Event Called when Chat Dialog Box is Closed by user. Same as exit event of Menu.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChatDialog_FormClosing(Object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
#endregion
#region StateObject Class Definition
/// <summary>
/// StateObject Class to read data from Client
/// </summary>
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
#endregion
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Net.Sockets;
Net系统;
使用系统线程;
命名空间聊天客户端
{
公共部分类Form1:Form
{
字节[]数据=新字节[1024];
字符串输入;
国际港口;
TcpClient服务器;
int recv;
公共活动表单ClosedEventHandler表单Closed;
字符串数据;
公共表格1()
{
初始化组件();
形式_Load();
}
///
///
///
公共作废表格(加载)
{
rtbContent.AppendText(“请输入服务器的端口号:\n”);
}
///
///函数使用端口号连接到服务器
///
///
///
私有void btnConnect_单击(对象发送者,事件参数e)
{
输入=txtPort.Text;
port=Int32.Parse(输入);
尝试
{
服务器=新的TcpClient(“127.0.0.1”,端口);
}
捕获(SocketException)
{
rtbContent.AppendText(“无法连接到服务器”);
返回;
}
rtbContent.AppendText(“已连接到服务器…\n”);
}
///
///函数将数据发送到服务器。
///
///
///
私有无效BTN发送\单击(对象发送方,事件参数e)
{
NetworkStream ns=server.GetStream();
StateObject状态=新的StateObject();
state.workSocket=server.Client;
server.Client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
新的AsyncCallback((新Form1()).OnReceive),状态);
输入=rtbSend.Text;
如果(输入=“退出”)
返回;
ns.Write(Encoding.ASCII.GetBytes(input),0,input.Length);
ns.Flush();
rtbContent.AppendText(rtbSend.Text);
rtbSend.Text=“”;
接收数据(发送方,e);
ns.Close();
}
///
///服务器为客户端发送数据时调用的事件。
///
///
///
私有void receiveData(对象发送方,事件参数e)
{
NetworkStream ns=server.GetStream();
StateObject状态=新的StateObject();
server.Client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
新的AsyncCallback((新Form1()).OnReceive),状态);
数据=新字节[1024];
recv=ns.Read(数据,0,数据长度);
stringData=Encoding.ASCII.GetString(数据,0,recv);
rtbContent.AppendText(stringData);
}
///
///窗体关闭时调用的事件。
///
///
///
私有void Form1\u FormClosed(对象发送方,FormClosedEventArgs e)
{
AppendText(“从服务器断开连接…”);
server.Close();
MessageBox.Show(“FormClosed事件”);
}
///
///从客户端接收数据的异步回调函数
///
///
接收时公共无效(IAsyncResult ar)
{
String content=String.Empty;
//检索状态对象和处理程序套接字
//从异步状态对象。
StateObject状态=(StateObject)ar.AsyncState;
套接字处理程序=state.workSocket;
int字节读取;
if(handler.Connected)
{
//从客户端套接字读取数据。
尝试
{
bytesRead=handler.EndReceive(ar);
如果(字节读取>0)
{
//可能会有更多数据,因此请存储到目前为止收到的数据。
陈述某人移除(0,陈述某人长度);
state.sb.Append(Encoding.ASCII.GetString(
state.buffer,0,bytesRead));
//在富文本框中显示文本
content=state.sb.ToString();
rtbContent.AppendText(content.ToString());
handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
新的异步回调(OnReceive),状态);
}
}
catch(SocketException SocketException)
{
//WSAECONNRESET,另一方不礼貌地关闭
如果(socketException.ErrorCode==10054 | |((socketException.ErrorCode!=10004)和&(socketException.ErrorCode!=10053)))
{
handler.Close();
}
}
//吃光例外。。。。
捕获(异常)
{
MessageBox.Show(exception.Message+“\n”+exception.StackTrace);
}
}
}
}
///
///服务器接收状态
///
公共类状态对象
{
//客户端套接字。
公共套接字工作组=null;
//接收缓冲区的大小。
public const int BufferSize=1024;
//接收缓冲区。
公共字节[]缓冲区=新字节[BufferSize];
//接收到的数据字符串。