C#中socket通信问题
我这样只能接收一次消息,怎么持续接收?怎么发送?TcpListener和Socket中的listen有啥区别?...
我这样只能接收一次消息,怎么持续接收?怎么发送?TcpListener和Socket中的listen有啥区别?
展开
4个回答
展开全部
下面的代码只是服务端的,客户端的类似了哈!!
/// <summary>
/// 开启监听线程方法
/// </summary>
/// <param name="port">端口</param>
private void ServerThread(object port)
{
try
{
IPEndPoint point = new IPEndPoint(IPAddress.Any, (int)port);//创建socket端点
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(point);
serverSocket.Listen(10);
while (true)
{
try
{
Socket ClientSocket = serverSocket.Accept();
IPEndPoint clientep = (IPEndPoint)ClientSocket.RemoteEndPoint;
new Thread(ClientThread).Start(ClientSocket);
}
catch (Exception ex)
{
serverSocket.Close();
MessageBox.Show(ex.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 接收数据线程方法
/// </summary>
/// <param name="socket">目标客户端</param>
private void ClientThread(object socket)
{
Socket Client = socket as Socket;
try
{
int BufferLength = 1024;
while (true)
{
if ((BufferLength = Client.Available) != 0)
{
byte[] buffer = new byte[BufferLength];
Client.Receive(buffer, 0, BufferLength, SocketFlags.None);// 接收
//doSomeThing......
}
Thread.Sleep(1);
}
}
catch (Exception ex)
{
if (!ex.GetType().Equals(typeof(ObjectDisposedException)))
{
Client.Close();
Client.Dispose();
}
}
}
调用
private void button1_Click(object sender, EventArgs e)
{
new Thread(ServerThread).Start((12345);
button1.Enabled = false;
}
/// <summary>
/// 开启监听线程方法
/// </summary>
/// <param name="port">端口</param>
private void ServerThread(object port)
{
try
{
IPEndPoint point = new IPEndPoint(IPAddress.Any, (int)port);//创建socket端点
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(point);
serverSocket.Listen(10);
while (true)
{
try
{
Socket ClientSocket = serverSocket.Accept();
IPEndPoint clientep = (IPEndPoint)ClientSocket.RemoteEndPoint;
new Thread(ClientThread).Start(ClientSocket);
}
catch (Exception ex)
{
serverSocket.Close();
MessageBox.Show(ex.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 接收数据线程方法
/// </summary>
/// <param name="socket">目标客户端</param>
private void ClientThread(object socket)
{
Socket Client = socket as Socket;
try
{
int BufferLength = 1024;
while (true)
{
if ((BufferLength = Client.Available) != 0)
{
byte[] buffer = new byte[BufferLength];
Client.Receive(buffer, 0, BufferLength, SocketFlags.None);// 接收
//doSomeThing......
}
Thread.Sleep(1);
}
}
catch (Exception ex)
{
if (!ex.GetType().Equals(typeof(ObjectDisposedException)))
{
Client.Close();
Client.Dispose();
}
}
}
调用
private void button1_Click(object sender, EventArgs e)
{
new Thread(ServerThread).Start((12345);
button1.Enabled = false;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
您好,这样的:
TCP是面向连接的,服务端接收到连接(Socket client = server.Accept() )后,表示连接已经建立好,就可以相互通讯了。服务端用Accept到的client发送消息就行了,客户端不需要再Listen,直接Receive消息就行了(用你建立连接的Socket接收)。客户端发完消息不要关闭掉,用它来接收服务端的消息。
TCP是面向连接的,服务端接收到连接(Socket client = server.Accept() )后,表示连接已经建立好,就可以相互通讯了。服务端用Accept到的client发送消息就行了,客户端不需要再Listen,直接Receive消息就行了(用你建立连接的Socket接收)。客户端发完消息不要关闭掉,用它来接收服务端的消息。
追问
给点代码实例看看,谢谢了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你需要一个线程,完成接收操作
追问
我也觉得,可是线程也刚学会不熟,求个小实例
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-05-08
展开全部
要不停的监听
追问
能具体点么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询