作者sendo3022 (giam)
站内C_Sharp
标题[问题] socket 连线问题
时间Thu Dec 6 14:03:20 2007
目前写出一用户端程式,在与伺服端的连结中
出现
连线尝试失败,因为连线对象有一段时间并未正确回应
或是连线建立失败,因为连线的主机无法回应。
的反应
complier有过~~
在伺服端的执行已经开启
却还是出现此样的反应
目前是使用C#来做SOCKET无线网路来做传输
使用无线AP分享器与无线网卡来作媒介
问题有可能出现在IP还是PORT的地方吗??
先谢谢
以下是程式码
amespace ConsoleApplication1
{
class Program
{
public static void StartClient() {
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = IPAddress.Parse("192.168.182.12");//ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,5712);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Connect the socket to the remote endpoint. Catch any errors.
try {
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes,0,bytesRec));
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
} catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.71.97.105
※ 编辑: sendo3022 来自: 210.71.97.105 (12/06 14:04)
1F:推 reptile0426:不一定是client的问题 也有可能是server的问题 12/06 16:16
2F:→ reptile0426:要不要都po上来试试,而且似乎用tcpclient会比较好写 12/06 16:17
3F:推 sendo3022:已经解决罗~~感谢版上的回答~~~原因出在IP被锁 ><感恩 12/07 14:34