我想通过套接字在iPhone和C#服务器之间进行通信,
当我尝试使用cocoaasynsockets通过LAN创建从iPhone到我的C Sharp服务器的套接字时,但它没有在服务器上创建任何套接字。我尝试提供不同的IP,例如在(whatispmyip.com)上找到的127.0.0.1/192.168.2.102(local)/IP,但没有收获,
这是我的iPhone测试代码
- (IBAction)connect:(id)sender{
GCDAsyncSocket *socket;
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![socket connectToHost:@"192.168.2.102" onPort:52523 error:&err]) // Asynchronous!
{
NSLog(@error : %@"", err);
}
}
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"connected");
}
这是我的C Sharp服务器代码
TcpListener _tcpListerner;
public void startingServer() {
// IPAddress ipad = Dns.Resolve("localhost").AddressList[0];
IPAddress ipad = IPAddress.Parse("192.168.2.102");
_tcpListerner = new TcpListener(ipad, 52523);
_tcpListerner.Start();
Console.WriteLine("The local End point is :" +_tcpListerner.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Thread thread = new Thread(acceptClients);
thread.Start();
}
void acceptClients()
{
Socket s = _tcpListerner.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
char cc = ' ';
string test = null;
Console.WriteLine("Recieved...");
for (int i = 0; i < k - 1; i++)
{
Console.Write(Convert.ToChar(b[i]));
cc = Convert.ToChar(b[i]);
test += cc.ToString();
}
我缺少的是什么,我应该提供什么IP,或者在服务器端或iphone(客户端)上有一些设置可以接受/创建来自网络的套接字
最佳答案
在服务器上使用 IPAddress.Any
监听所有计算机地址。
在客户端上,使用要连接的服务器的LAN地址。
关于c# - cocoaasynsocket到C Sharp服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11595551/