Hallo,
ich versuche eine einfache Kommunikation mittels TCP zwischen AutoIt Client und Java Server. Das Senden vom Server zum Client klappt ohne Probleme, jedoch geht es andersherum nicht.
Hier der Quellcode.
Client:
Spoiler anzeigen
; -------------------------------------------------------------------------
; Imports
; -------------------------------------------------------------------------
#include <FF.au3>
[/autoit] [autoit][/autoit] [autoit]; -------------------------------------------------------------------------
; Variables
; -------------------------------------------------------------------------
Global $connectionProps[2]
Global $tcpSocket, $ffSocket
Local Const $IniPath = @ScriptDir & "\config.ini"
Local $tcp, $data
; -------------------------------------------------------------------------
; Main
; -------------------------------------------------------------------------
; import program configs
If FileExists($IniPath) Then
$connectionProps[0] = IniRead($IniPath, "Connection", "Host", "NotFound")
$connectionProps[1] = IniRead($IniPath, "Connection", "Port", "10101")
SplashTextOn("","Configuration file found.",300,50,-1,-1,1)
Sleep(3000)
Else
SplashTextOn("","No config files.",300,50,-1,-1,1)
Sleep(3000)
Exit(0)
EndIf
; Firefox connection attempt
$ffSocket = _FFConnect(Default,Default,3000)
If $ffSocket > -1 Then
SplashTextOn("","Connection to FireFox established.",300,50,-1,-1,1)
Sleep(3000)
SplashOff()
Else
SplashTextOn("","Unable to connect to FireFox.",300,50,-1,-1,1)
Sleep(3000)
Exit(0)
EndIf
; TCP connection attempt
$tcp = TCPStartup()
If $tcp = 0 Then
SplashTextOn("","Unable to startup TCP Services.",300,50,-1,-1,1)
Sleep(3000)
Exit(0)
EndIf
$tcpSocket = TCPConnect($connectionProps[0],$connectionProps[1])
If $tcpSocket > -1 Then
SplashTextOn("","TCP Connection established.",300,50,-1,-1,1)
Sleep(3000)
SplashOff()
Else
SplashTextOn("","Unable to connect to Port " & $connectionProps[1],300,50,-1,-1,1)
Sleep(3000)
Exit(0)
EndIf
;ready for work
TCPSend($tcpSocket, "ready")
If @error Then Exit(0)
; main loop
While 1
;TCPSend($tcpSocket, "ready")
;If @error Then Exit(0)
$data = TCPRecv($tcpSocket, 512)
If $data <> "" Then
MsgBox(0, "Client: Received Packet", $data)
EndIf
WEnd
[/autoit] [autoit][/autoit] [autoit]; -------------------------------------------------------------------------
; Functions
; -------------------------------------------------------------------------
Server:
Spoiler anzeigen
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread {
private Socket clientSocket;
public Server(Socket clSocket) {
clientSocket = clSocket;
start();
}
public void run() {
String line;
System.out.println("Starting Thread");
try {
System.out.print("Connection from: " + clientSocket.getLocalAddress());
System.out.println(":" + clientSocket.getLocalPort());
System.out.print("to " + clientSocket.getInetAddress());
System.out.println(":" + clientSocket.getPort());
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintStream out = new PrintStream(clientSocket.getOutputStream());
out.println("Your are connected");
while(true) {
line = in.readLine();
//Scanner in = new Scanner(clientSocket.getInputStream());
//line = in.nextLine();
System.out.println("Message: " + line);
if(line.equals("")) break;
else out.println(line);
}
System.out.println("Sopping Thread");
} catch(Exception e) {e.printStackTrace();}
}
public static void main(String[] args) {
int port = 11111;
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Starting Server");
while(true) {
Socket clSocket = serverSocket.accept();
new Server(clSocket);
}
} catch (IOException e) {e.printStackTrace();}
}
}
Alles anzeigen
Als Test habe ich mal einen Echo Server in Python geschrieben und siehe da, es hat reibungslos geklappt. Vielleicht hat jemand eine Idee wieso es mit Java nicht geht.
Gruß
rororo