我在Windows窗体应用程序中实现聊天套接字程序。当我第一次通过发送按钮发送数据时,服务器可以从客户端接收数据,但是第二次不能。它错误"
无法访问已处置的对象。对象名称: System.Net.Socket.TcpClient
" 如何解决?
我的客户代码:
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;
string stringData;
public Form1()
{
InitializeComponent();
Form_Load();
}
/// <summary>
///
/// </summary>
public void Form_Load()
{
rtbContent.AppendText("Please Enter the port number of Server:\n");
//rtbContent.Text = "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");
//rtbContent.Text = "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)
{
try
{
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;
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
//ns.Flush();
// data = new byte[1024];
// recv = ns.Read(data, 0, data.Length);
// stringData = Encoding.ASCII.GetString(data, 0, recv);
//Console.WriteLine(stringData);
//rtbContent.AppendText(rtbSend.Text);
//rtbContent.AppendText(rtbSend.Text);
rtbSend.Text = "";
//receiveData(sender, e);
ns.Close();
server.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Send Button", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
}
}
/// <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.Text = "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();
}
}
答案 0 :(得分:3)
server.Close();
当您使用第一个btnSend_Click
方法发送数据时,您正在关闭连接。如果从方法中省略它,它可以解决您的问题。
ns.Close();
您可能也需要省略它。