Create server TCP in Winodws Univeral App (Javascript) and client Android

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To create a TCP server in a Windows Universal App using JavaScript and a TCP client in Android, you can use the following steps:

  1. Create TCP Server (Windows Universal App - JavaScript):
    • In your Windows Universal App, you can use the Windows.Networking.Sockets namespace to create a TCP server. This namespace allows you to create socket objects for communication.
    • First, make sure you have the necessary capabilities in your app manifest to enable network communication. Add the "Internet (Client & Server)" capability to allow the app to act as a server.
    • Then, use the following JavaScript code to create a TCP server:
javascript
// Import Windows.Networking.Sockets namespace
var sockets = Windows.Networking.Sockets;

// Create a socket listener
var serverListener = new sockets.StreamSocketListener();

// Bind the listener to a specific port
serverListener.bindServiceNameAsync("12345").done(
function () {
// Listen for connections
serverListener.addEventListener("connectionreceived", function (args) {
// Handle the connection
var inputStream = args.socket.inputStream;
var dataReader = new Windows.Storage.Streams.DataReader(inputStream);

// Read incoming data from the client
dataReader.loadAsync(1024).done(function (count) {
var receivedData = dataReader.readString(count);
// Process the received data here

// Send a response back to the client (optional)
var outputStream = args.socket.outputStream;
var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream);
dataWriter.writeString("Server received your data.");
dataWriter.storeAsync().done(function () {
dataWriter.close();
});
});
});
},
function (error) {
console.error("Error binding the socket: " + error.message);
}
);
  1. Create TCP Client (Android):
    • In your Android app, you can use the java.net.Socket class to create a TCP client and connect to the TCP server running on the Windows Universal App.
    • Use the following Java code to create a TCP client:
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpClient {
public static void main(String[] args) {
try {
String serverIpAddress = "192.168.1.100"; // Replace with the IP address of your Windows Universal App
int serverPort = 12345; // Replace with the port number used by the TCP server

// Create a new socket and connect to the server
Socket socket = new Socket(serverIpAddress, serverPort);

// Create reader and writer for socket communication
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Send data to the server
out.println("Hello, server!");

// Read response from the server
String response = in.readLine();
System.out.println("Server response: " + response);

// Close the socket when done
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Note: Replace "192.168.1.100" with the IP address of your Windows Universal App server and "12345" with the port number you used for the server.

With these code snippets, you can create a TCP server in your Windows Universal App and a TCP client in your Android app, allowing them to communicate over the network.