Answer the question
In order to leave comments, you need to log in
How to connect a TCP client written in Java Android Studio to a TCP server written in C#?
Wrote TCP server and client in C#. Everything works: the client connects to the server and the server accepts the connection from the client. Then I decided to make a client in Java Android Studio. I made a connection via socket on my local ip and common port (same as on the server). And the line of code with the connection: Socket s = new Socket("192.168.43.183", 8080) does not work: the connection does not work stupidly.
Here is the server code in C#
public class ServerObject
{
static TcpListener tcpListener; // сервер для прослушивания
List<ClientObject> clients = new List<ClientObject>(); // все подключения
protected internal void AddConnection(ClientObject clientObject)
{
clients.Add(clientObject);
}
protected internal void RemoveConnection(string id)
{
// получаем по id закрытое подключение
ClientObject client = clients.FirstOrDefault(c => c.Id == id);
// и удаляем его из списка подключений
if (client != null)
clients.Remove(client);
}
// прослушивание входящих подключений
protected internal void Listen()
{
try
{
tcpListener = new TcpListener(IPAddress.Any, 8080);
tcpListener.Start();
Console.WriteLine("Сервер запущен. Ожидание подключений...");
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
ClientObject clientObject = new ClientObject(tcpClient, this);
Thread clientThread = new Thread(new ThreadStart(clientObject.Process));
clientThread.Start();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Disconnect();
}
}
// трансляция сообщения подключенным клиентам
protected internal void BroadcastMessage(string message, string id)
{
byte[] data = Encoding.Unicode.GetBytes(message);
for (int i = 0; i < clients.Count; i++)
{
clients[i].Stream.Write(data, 0, data.Length); //передача данных
}
}
// отключение всех клиентов
protected internal void Disconnect()
{
tcpListener.Stop(); //остановка сервера
for (int i = 0; i < clients.Count; i++)
{
clients[i].Close(); //отключение клиента
}
Environment.Exit(0); //завершение процесса
}
}
public class MainActivity extends AppCompatActivity {
private EditText ipeditText;
private EditText nicknameText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnButtonClick1 (View view) {
ipeditText = (EditText) findViewById(R.id.ipeditText);
nicknameText = (EditText) findViewById(R.id.nameText);
String ip = ipeditText.getText().toString();
String nick = nicknameText.getText().toString();
BackGroundTask b1 = new BackGroundTask();
b1.execute(ip, nick);
try {
Socket s = new Socket("192.168.43.183", 8080);
PrintWriter printWriter = new PrintWriter(s.getOutputStream());
printWriter.write(nick);
printWriter.flush();
printWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
// String ip = ipeditText.getText().toString();
// try {
// s = new Socket(ip, 8080);
// printWriter = new PrintWriter(s.getOutputStream());
// } catch (IOException e) {
// e.printStackTrace();
// }
Intent intent = new Intent(this, DisplayChatActivity.class);
startActivity(intent);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question