Пытаюсь написать клиент-сервер
2 машины объеденены по локальной сети. Через браузер могу общаться, но соединение через сокеты не могу установить. Для теста написал немного кода
сервер
| Код | using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets;
namespace server { class Program { static void Main(string[] args) { Int32 port = 0x732; TcpListener server = new TcpListener(port); //TcpListener server = new TcpListener(ip, port); server.Start();
while(true) { Console.Write("Waiting for a connection... "); TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); NetworkStream stream = client.GetStream(); client.Close(); } } } }
|
клиент
| Код | using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets;
namespace client { class Program { private static int m_port = 0x732; private static TcpClient m_client = null; protected static NetworkStream m_stream = null;
static void Main(string[] args) { IPHostEntry hostEntry = Dns.GetHostEntry("my_computer"); IPAddress[] ipList = hostEntry.AddressList;
for (int index = 0; index < ipList.Length; ++index) { IPAddress ip = ipList[index]; IPEndPoint endPoint = new IPEndPoint(ip, m_port);
try { m_client = new TcpClient(endPoint); m_stream = m_client.GetStream(); Console.WriteLine(ip.ToString() + ": " + "Connect!"); } catch (ArgumentNullException ex) { Console.WriteLine(ip.ToString() + ": ", ex); } catch (SocketException ex) { Console.WriteLine(ip.ToString() + ": ", ex); } }
System.Threading.Thread.Sleep(5000);
} } }
|
Говорит, что "Требуемый адрес для своего контекста неверен"
Firewall отключен. Что еще может быть ? |