我用一台Gps手持机和pc通过socket连接
pc当主机,手持机当客户端
主机代码如下: IPAddress ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 34567);
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Bind(ipEndPoint );
ListenThread = new Thread(ListenClientMessage);
ListenThread.Start();
ServerSocket.Listen(0);
MessageBox.Show("服务器打开");
Socket socket = ServerSocket.Accept();
if (socket != null)
。。客户端代码如下:
IPAddress ipAddress = IPAddress.Parse("192.168.0.105");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 34567);
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ClientSocket.Connect(ipEndPoint);//运行到这就提示 主机主动拒绝
MessageBox.Show("连接服务器成功");
……每次只要 手持机通过数据线连在pc上
运行 连接 就提示 主机主动拒绝
我通过 ipconfig 可以获得 pc 和手持机的ip
并且可以ping 通
这个图和解决啊 求助啊
pc当主机,手持机当客户端
主机代码如下: IPAddress ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 34567);
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Bind(ipEndPoint );
ListenThread = new Thread(ListenClientMessage);
ListenThread.Start();
ServerSocket.Listen(0);
MessageBox.Show("服务器打开");
Socket socket = ServerSocket.Accept();
if (socket != null)
。。客户端代码如下:
IPAddress ipAddress = IPAddress.Parse("192.168.0.105");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 34567);
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ClientSocket.Connect(ipEndPoint);//运行到这就提示 主机主动拒绝
MessageBox.Show("连接服务器成功");
……每次只要 手持机通过数据线连在pc上
运行 连接 就提示 主机主动拒绝
我通过 ipconfig 可以获得 pc 和手持机的ip
并且可以ping 通
这个图和解决啊 求助啊
解决方案 »
- c# treeview 选中某项后,焦点移开后,比如焦点到文本框里了,此时发现treeview刚才选中的某项的选中状态消失了。
- 有關Socket異步通訊的問題——數據只有第一次發送成功
- 请教大家1个问题
- 如何实现保存和读取一个数据集(类似配置文件)?
- 表单提交向数据库添加记录的问题
- 跪求...如何实现类似于SqlServer中视图设计器的动态连接线.....在线等
- 怎样通过反射判断一个类是"internal"类
- 有时候return后面什么都不带``是什么意思呢``
- 要做自己的customers集合类,相关的几个似懂非懂的送分弱问题???
- 未将对象引用设置到对象的实例。咋回事?
- 有两个小问题。高手你在哪里
- 当我使用xlChart.CopyPicture,把东西放到内存后,我想知道我放到内存中的这个东西,是什么格式的?怎么通过C#做到这点呢?
试试通不通检查防火墙是否屏蔽了该端口
ip地址最好不要直接指定为第一个,或者你有多个网卡呢
listen()多个连接看看
不行单步调试看看那句报错的
Socket s = null;
TcpClient clientsocket;
NetworkStream ns;
TcpListener tcpListener = null;
TcpClient tcpClient = null;
private System.Threading.Thread pcThread = null;
private static bool IsDone = true;
if (tcpListener == null)
{
int port = 34567;
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port); tcpListener = new TcpListener(localEndPoint);
tcpListener.Start(10); if (pcThread==null)
pcThread = new System.Threading.Thread(new System.Threading.ThreadStart(StartServer));
pcThread.Start();
}
private void StartServer()
{
while (IsDone)
{
string data = null;
try
{
tcpClient = tcpListener.AcceptTcpClient();
tcpClient.ReceiveTimeout = 240 * 1000;
ns = tcpClient.GetStream();
byte[] recvBytes = new byte[1024];
int bytes = ns.Read(recvBytes, 0, recvBytes.Length);//从客户端接受信息
if (bytes > 0)
{
data = Encoding.GetEncoding("GBK").GetString(recvBytes, 0, bytes);
//TO DO
// //返回响应
byte[] bSend = Encoding.GetEncoding("GBK").GetBytes("SUCCESS");
ns.Write(bSend, 0, bSend.Length);
}
}
catch (SocketException ex)
{
//
}
catch (Exception ex)
{
//
}
finally
{
if (ns != null)
{
ns.Dispose();
ns.Close();
ns = null;
}
if (tcpClient != null)
{
tcpClient.Close();
tcpClient = null;
}
}
System.Threading.Thread.Sleep(500);
}
}