代码如下,主要是ServiceClient() 线程里面 while(true)这个循环造成的,不知道怎么写服务器端程序,求仙人指路。
尝试在ServiceClient()线程里面,让线程睡眠,但是这样出现客户端发来的数据,服务器端好长时间才可以收到。高人有什么其他的服务器端详细写法,贴上来。using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.Net;/// <summary>
///Service 的摘要说明
/// </summary>
public class TCPService
{ static int listenport = 2223;
Socket clientsocket;
List<Client> clients;
Thread clientservice;
Thread threadListen;
static bool IsBegin = false; public TCPService()
{
if (IsBegin == false)
{
clients = new List<Client>();
threadListen = new Thread(new ThreadStart(StartListening));
threadListen.Start();
IsBegin = true;
}
} private void StartListening()
{
TcpListener listener = new TcpListener(listenport);
listener.Start();
while (true)
{
try
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch (Exception ex)
{
}
}
}
private void ServiceClient()
{
Socket client = clientsocket; if (client.Connected)
{
//在这里不停的监听客户端发送的数据所以cpu是100%已经被使用
while (true)
{
Byte[] buffer = new Byte[1024];
int bufLen = 0;
try
{
bufLen = client.Available;
client.Receive(buffer, 0, bufLen, SocketFlags.None);
if (bufLen == 0) {
continue;
}
}
catch (Exception ex)
{
return;
}
//获取客户端消息
string clientcommand = System.Text.Encoding.UTF8.GetString(buffer).Substring(0, bufLen);
}
}
}
private void SendToClient(Client cl, string clientCommand)
{
Byte[] message = System.Text.Encoding.UTF8.GetBytes(clientCommand);
Socket s = cl.Sock;
if (s.Connected)
{
s.Send(message, message.Length, 0);
}
}}
尝试在ServiceClient()线程里面,让线程睡眠,但是这样出现客户端发来的数据,服务器端好长时间才可以收到。高人有什么其他的服务器端详细写法,贴上来。using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.Net;/// <summary>
///Service 的摘要说明
/// </summary>
public class TCPService
{ static int listenport = 2223;
Socket clientsocket;
List<Client> clients;
Thread clientservice;
Thread threadListen;
static bool IsBegin = false; public TCPService()
{
if (IsBegin == false)
{
clients = new List<Client>();
threadListen = new Thread(new ThreadStart(StartListening));
threadListen.Start();
IsBegin = true;
}
} private void StartListening()
{
TcpListener listener = new TcpListener(listenport);
listener.Start();
while (true)
{
try
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch (Exception ex)
{
}
}
}
private void ServiceClient()
{
Socket client = clientsocket; if (client.Connected)
{
//在这里不停的监听客户端发送的数据所以cpu是100%已经被使用
while (true)
{
Byte[] buffer = new Byte[1024];
int bufLen = 0;
try
{
bufLen = client.Available;
client.Receive(buffer, 0, bufLen, SocketFlags.None);
if (bufLen == 0) {
continue;
}
}
catch (Exception ex)
{
return;
}
//获取客户端消息
string clientcommand = System.Text.Encoding.UTF8.GetString(buffer).Substring(0, bufLen);
}
}
}
private void SendToClient(Client cl, string clientCommand)
{
Byte[] message = System.Text.Encoding.UTF8.GetBytes(clientCommand);
Socket s = cl.Sock;
if (s.Connected)
{
s.Send(message, message.Length, 0);
}
}}
解决方案 »
- WCF 的UdpDiscoveryEndpoint老报异常,急~~~~~~~~~~~~~~~
- 如何把断句后的字符串全组合
- C# chart控件时间轴如何放大
- 获取网页源码问题,可以指教下吗
- 【简单的listView问题】2个listview的项怎么关联起来,在线等,解决就结帖,白送分啦
- 文件双向同步 基于FileSystemWatcher
- 编程难度系数 9
- 分享个读取INI文件类 不要分 需要的下载
- ■■■如何在2000下做出XP风格的程序?同时也是在2000下运行!不是放到XP上运行!■■■
- 谁有关于命名空间namespace的参考手册???
- WPF 数据绑定问题
- Castle IOC或spring.net依赖注入好像没有反射功能?
Byte[] buffer = new Byte[1024];
int bufLen = 0;
try
{
bufLen = client.Available;
client.Receive(buffer, 0, bufLen, SocketFlags.None);
if (bufLen == 0) {
continue;
}
}修改为 Byte[] buffer = new Byte[1024];
try
{
int buflen = client.Receive(buffer, 0, 1024, SocketFlags.None);
if (bufLen == 0) {
return;
}
}
全给分了。能否在这里给大家说一下为什么要这样改
求大仙,略讲原理,,,我的代码是从网上抄来的。所以有些地方还是不太懂如果有其他 高人讲为什么会是100%,另外分数也送上了
bool isWatch = true; #region 1.被线程调用 监听连接端口
/// <summary>
/// 被线程调用 监听连接端口
/// </summary>
void StartWatch()
{
while (isWatch)
{
//threadWatch.SetApartmentState(ApartmentState.STA);
//监听 客户端 连接请求,但是,Accept会阻断当前线程
Socket sokMsg = sokWatch.Accept();//监听到请求,立即创建负责与该客户端套接字通信的套接字
ConnectionClient connection = new ConnectionClient(sokMsg, ShowMsg, RemoveClientConnection);
//将负责与当前连接请求客户端 通信的套接字所在的连接通信类 对象 装入集合
dictConn.Add(sokMsg.RemoteEndPoint.ToString(), connection);
//将 通信套接字 加入 集合,并以通信套接字的远程IpPort作为键
//dictSocket.Add(sokMsg.RemoteEndPoint.ToString(), sokMsg);
//将 通信套接字的 客户端IP端口保存在下拉框里
cboClient.Items.Add(sokMsg.RemoteEndPoint.ToString());
ShowMsg("接收连接成功......");
//启动一个新线程,负责监听该客户端发来的数据
//Thread threadConnection = new Thread(ReciveMsg);
//threadConnection.IsBackground = true;
//threadConnection.Start(sokMsg);
}
}
#endregion
我的上面代码能否使用阻塞的方法。