Answer the question
In order to leave comments, you need to log in
How to connect via Ip from a phone (client) to a computer (server)?
In general, this is the problem, I have a client and a server on the computer, it works like a chat (written in C ++), now I found such a thing "Xamarin" that allows you to compile code from c # to the android platform.
I set everything up and started trying to connect from the phone to the server on the computer, the phone is connected to Wi-Fi like the computer. When I enter IPs, the phone gives an error connecting to the server.
I don't know how to make it work properly, maybe someone can help.
Here is the c# code (client on phone):
static void TryConnect(int port)
{
// Буфер для входящих данных
byte[] bytes = new byte[50];
// Соединяемся с удаленным устройством
// Устанавливаем удаленную точку для сокета
IPHostEntry ipHost = Dns.GetHostEntry("localhost");
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, port);
Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// Соединяем сокет с удаленной точкой
sender.Connect(ipEndPoint);
string message = "Hello dima";
byte[] msg = Encoding.UTF8.GetBytes(message);
int bytesSent = sender.Send(msg);
////
}
//---- Build address structure to bind to socket.--------
memset(&channel, 0, sizeof(channel));// zerochannel
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = inet_addr(adrr); //htonl(INADDR_ANY)
channel.sin_port = htons(SERVER_PORT);
//--------------------------------------------------------
// ---- create SOCKET--------------------------------------
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s < 0) {
textBox1->Text = "socket error :" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
//---------------------------------------------------------
//---- BIND socket ----------------------------------------
b = bind(s, (struct sockaddr *) &channel, sizeof(channel));
if (b < 0) {
textBox1->Text = "bind error :" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
//----------------------------------------------------------
//---- LISTEN socket ----------------------------------------
l = listen(s, QUEUE_SIZE); // specify queue size
if (l < 0) {
textBox1->Text = "listen error %ld" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
//-----------------------------------------------------------
//---- ACCEPT connection ------------------------------------
sa = accept(s, 0, 0); // block for connection request
if (sa < 0) {
textBox1->Text = "accept error " + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
else {
textBox1->Text = "connection accepted";
}
Answer the question
In order to leave comments, you need to log in
You can close the topic, I found a solution. It took the night for everything, the problem was solved in this way:
TcpClient client; // Creates a TCP Client
client = new TcpClient("192.168.0.103 ", 11000);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question