积极答复者
简单的SOCKET通信问题

问题
-
1、实现一个简单的SOCKET通信程序
启动服务端,由客户端发起连接,连通后向服务端发送连通时间
2、问题(如代码):
这里ServerIP改成127.0.0.1,客户端程序是可以执行与服务端的通信的,但如果改成我的机器IP时,就不通过,问题停留在“ int Bytes = Strm.Read(inputBuffer, 0, inputBuffer.Length);”这句话上。
3、代码:(客户端)
TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(ServerIP, 139));
//获取流Stream Strm;
try
{
Strm = client.GetStream();
}
catch (InvalidOperationException)
{
label3.Text = String.Format("Cannot Connect to Server :{0}", ServerTextBox.Text);
return;
}
catch (SocketException exc)
{
StringBuilder strB = new StringBuilder("");
strB.Append(String.Format("Cannot connect to Server :{0}\r\n", ServerTextBox.Text));
strB.Append(exc.Message + "\r\r");
strB.Append("Socket Error Code:" + exc.ErrorCode.ToString());
label3.Text = strB.ToString();
return;
}
//读取流,并转ASCII码
Byte[] inputBuffer = new Byte[128];
int Bytes = Strm.Read(inputBuffer, 0, inputBuffer.Length);
谢谢!!
答案
-
这是我以前写的:通讯没有出过我问题,
//设置编号方式
Encoding myEncoding = Encoding.GetEncoding("GB2312");
//发送消息
int port = 10000;//端口号
string host = "192.168.0.0";//ip地址
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
Socket SockSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
SockSend.Connect(ipe);//连接到服务器
byte[] SendString = myEncoding.GetBytes(sendStr);
SockSend.Send(SendString, SendString.Length, 0);//发送消息
//接收消息
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = SockSend.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += myEncoding.GetString(recvBytes, 0, bytes);- 已标记为答案 YiChun Chen 2010年1月26日 8:21
全部回复
-
把服务器端的监听IP地址改成0.0.0.0即IPAddress.Any
Any 字段指示 Socket 实例应侦听所有网络接口上的客户端活动
Memory all alone in the moonlight~My Blog in .NET~~~ -
服务端程序如下:请问你说的IpAddress.Any放在哪些 ,谢谢
TcpListener tcpServer = new TcpListener(139);
tcpServer.Start();//Report所在位置
IPHostEntry thisHost = Dns.Resolve(Dns.GetHostName());
Console.WriteLine("Host:{0} listening on : {1},port: {2}", thisHost.HostName, thisHost.AddressList[0].ToString(), 139);
Console.WriteLine("Waiting for the connecting...");//接收阻塞,直到有人连接
TcpClient clientConn = tcpServer.AcceptTcpClient();
//发回包括时间在内的消息
Response = "Server Response sent at " + DateTime.Now.ToLongTimeString();//将字符串转换成字符数组,然后发送该数组
byte[] byteResponse = Encoding.ASCII.GetBytes(Response.ToCharArray());
clientConn.GetStream().Write(byteResponse, 0, byteResponse.Length);
clientConn.Close(); -
这是我以前写的:通讯没有出过我问题,
//设置编号方式
Encoding myEncoding = Encoding.GetEncoding("GB2312");
//发送消息
int port = 10000;//端口号
string host = "192.168.0.0";//ip地址
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
Socket SockSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
SockSend.Connect(ipe);//连接到服务器
byte[] SendString = myEncoding.GetBytes(sendStr);
SockSend.Send(SendString, SendString.Length, 0);//发送消息
//接收消息
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = SockSend.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += myEncoding.GetString(recvBytes, 0, bytes);- 已标记为答案 YiChun Chen 2010年1月26日 8:21