一个客户端向服务器端发送消息,可以接收并返回给该客户端消息,但一个客户端经过服务器端向另一个客户端发送消息就无法收到了,代码贴出。请熟悉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个未结帖子请尽快去结帖! 

解决方案 »

  1.   

    服务端代码是先receive然后再send,因为采用的是同步socket, 如果没有receive到数据的时候,就会阻塞住,所以“受消息的客户端”首先要发送一个消息过去,然后才能接受。
    另外监听后的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];
                    ..............
      

  2.   

    谢谢ls的,还有个问题要请教,在这里如何应用多线程(Thread),在vs.net2005环境下。
      

  3.   

    看看这个,好的话,楼主赏个分吧
    Socket开发之通讯协议及处理