一个客户端向服务器端发送消息,可以接收并返回给该客户端消息,但一个客户端经过服务器端向另一个客户端发送消息就无法收到了,代码贴出。请熟悉socket的tx赐教。代码如下//服务器端代码:
public class SynchronousSocketListener
{
public static string data = null;
public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress,2000);
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp );
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
Console.WriteLine("Waiting for a connection...");
Socket handler = listener.Accept();
data = null;
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
Console.WriteLine( "Text received : {0}", data);
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
} public static int Main(String[] args)
{
StartListening();
return 0;
}
} //发送消息客户端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace socekClient
{
class SendSocketClient
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
int port = 2000;
string host = "192.168.1.149";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
string sendStr = "hello!two";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
c.Close();
Console.ReadLine();
}
}
}
}
//接收消息的客户端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace socekClient
{
class ReceiveSocketClient
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
int port = 2000;
string host = "192.168.1.149";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine(recvStr);
c.Close();
Console.ReadLine();
} }
}}
+++++++++++++++++++++++++++++++++++++++++++++++++++
dhrubber 你好!你的结帖率是103.79% 你还有1个未结帖子请尽快去结帖!
public class SynchronousSocketListener
{
public static string data = null;
public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress,2000);
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp );
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
Console.WriteLine("Waiting for a connection...");
Socket handler = listener.Accept();
data = null;
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
Console.WriteLine( "Text received : {0}", data);
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
} public static int Main(String[] args)
{
StartListening();
return 0;
}
} //发送消息客户端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace socekClient
{
class SendSocketClient
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
int port = 2000;
string host = "192.168.1.149";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
string sendStr = "hello!two";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
c.Close();
Console.ReadLine();
}
}
}
}
//接收消息的客户端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace socekClient
{
class ReceiveSocketClient
{
[STAThread]
static void Main(string[] args)
{
while(true)
{
int port = 2000;
string host = "192.168.1.149";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine(recvStr);
c.Close();
Console.ReadLine();
} }
}}
+++++++++++++++++++++++++++++++++++++++++++++++++++
dhrubber 你好!你的结帖率是103.79% 你还有1个未结帖子请尽快去结帖!
解决方案 »
- 求大大帮忙看看用C#做批量改文件名时使用去除空格出现错误
- textBox,如何实现因在控件中输入内容,而导致控件上更改Text属性的值时引发的事件。
- 实现曲线连续打印
- 想写个在WEB页面上显示视频流的小东西,不知道如何入手
- 什么使域,字段,属性!!
- 急急急!!!!!!!!!!!!!!!!!
- 如何用c#.net聲明這個API函數(C轉C#)
- 如何检测程序是在开发环境还是用户运行环境
- 我做了一个C#程序,要拿到没有装过.net的机器上运行,对方机器要装什么呀,
- CrystalReport 如何在图表中动态指定图表的类型,设定它的可见性等!
- arraylist,为什么显示时总得到的是10,而不是1...10?!
- 关于WPF的Style
另外监听后的data = null去掉,否则清空保存的消息了。
修改了服务端和“受消息的客户端”的代码,“发消息的客户端”的代码没有改,改动如下:服务端:
..............
Socket handler = listener.Accept();
//data = null; //注释掉这个代码
bytes = new byte[1024];
..........受消息的客户端:
..............
string recvStr = "";
c.Send(Encoding.ASCII.GetBytes("According server logic, you need send first :) "));
byte[] recvBytes = new byte[1024];
..............
Socket开发之通讯协议及处理