rapot Posted February 18, 2011 Share Posted February 18, 2011 How i can send message between 2 computer by: autoIT client: $c_Ip = "127.0.0.1" $c_Port = 6879 TCPStartup() $socket = TCPConnect($c_Ip, $c_Port) if $socket = -1 Then msgbox(0, "Error", "Error Connecting") EndIf while True $msg = inputbox("Send msg", "Send msg to server") TCPSend($socket, $msg) sleep(1) WEnd Java server: expandcollapse popupimport java.net.*; import javax.swing.*; import java.io.*; import java.util.*; import java.awt.*; import java.io.BufferedReader; public class Server { public Server() { } public static void main(String[] args) { final int port = 6879; Socket clientsocket = null; ServerSocket sSocket = null; BufferedReader in = null; InputStream ins = null; BufferedReader inStream = null; String msg = ""; try{ sSocket = new ServerSocket(port); }catch(IOException e){ System.out.println("Error: " + e); } System.out.println("Starting server.."); try{ clientsocket = sSocket.accept(); }catch(IOException e){ System.out.println("Error: " + e); } System.out.println("Connection detected!"); try{ ins = clientsocket.getInputStream(); InputStreamReader blah = new InputStreamReader(ins); inStream = new BufferedReader(blah); }catch(IOException aE){ System.out.println("Error: " + aE ); } while(true){ try{ msg = inStream.readLine(); if(msg != ""){ System.out.println(msg); msg = ""; } ins = clientsocket.getInputStream(); System.out.println("e" + msg); }catch(IOException aE){ System.out.println("Error: " + aE); System.exit(0); } } } } Link to comment Share on other sites More sharing options...
Xenobiologist Posted February 18, 2011 Share Posted February 18, 2011 Why not using Autoit for both? Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
rapot Posted February 18, 2011 Author Share Posted February 18, 2011 I think AutoIt must be compatible with other languages to ease development. I tried embedding the library other languages into AutoIt Link to comment Share on other sites More sharing options...
Xenobiologist Posted February 18, 2011 Share Posted February 18, 2011 (edited) I think AutoIt must be compatible with other languages to ease development. I tried embedding the library other languages into AutoIt Hi, okay. I tried your code. And it works for me. Ran your server in eclipse and then this Script. (I changed the TCPSend to sending the text and a @crlf so that inStream.readLine(); knows there is an end. To try it, just change the port from 20000 to 6879 which you are using to connect and send some text. expandcollapse popup; MegaChat #include <File.au3> #include <GuiIPAddress.au3> #include <Inet.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> OnAutoItExitRegister("_endscript") Opt("MustDeclareVars", 1) Global $title = "MegaChat", $l_port_startValue = 10000, $r_port_startValue = 20000, $left = 100, $logPath = 'MegaChat.log' If WinExists($title) Then $title &= ' 2' $l_port_startValue = 20000 $r_port_startValue = 10000 $left += 500 $logPath = 'MegaChat2.log' EndIf Global $GUI Global $l_hostname_I, $l_ip_IC, $l_port_I, $listen_B Global $r_hostname_I, $r_ip_IC, $r_port_I, $connect_B Global $comm_E, $send_E, $send_B #Region ### START Koda GUI section ### Form=c:\dokumente und einstellungen\xf01145\eigene dateien\chat.kxf $GUI = GUICreate($title, 445, 508, $left, 111) GUICtrlCreateGroup("Local settings", 10, 10, 420, 105) GUICtrlCreateLabel("Hostname", 20, 30, 52, 17) $l_hostname_I = GUICtrlCreateInput(@ComputerName, 105, 25, 131, 21) GUICtrlCreateLabel("IP-address", 20, 55, 54, 17) $l_ip_IC = _GUICtrlIpAddress_Create($GUI, 105, 50, 130, 21) _GUICtrlIpAddress_Set($l_ip_IC, @IPAddress1) GUICtrlCreateLabel("Listener-Port", 20, 80, 63, 17) $l_port_I = GUICtrlCreateInput($l_port_startValue, 105, 75, 131, 21) $listen_B = GUICtrlCreateButton("Listen", 275, 25, 120, 25, $WS_GROUP) GUICtrlCreateGroup("", -99, -99, 1, 1) GUICtrlCreateGroup("Remote settings", 10, 115, 420, 105) GUICtrlCreateLabel("Hostname", 20, 135, 52, 17) $r_hostname_I = GUICtrlCreateInput("not connected ...", 105, 130, 131, 21) GUICtrlCreateLabel("IP-address", 20, 160, 54, 17) $r_ip_IC = _GUICtrlIpAddress_Create($GUI, 105, 155, 130, 21) _GUICtrlIpAddress_Set($r_ip_IC, @IPAddress1) GUICtrlCreateLabel("Listener-Port", 20, 185, 63, 17) $r_port_I = GUICtrlCreateInput($r_port_startValue, 105, 180, 131, 21) $connect_B = GUICtrlCreateButton("Connect", 280, 130, 120, 25, $WS_GROUP) GUICtrlCreateGroup("", -99, -99, 1, 1) GUICtrlCreateGroup("Communication summary", 10, 220, 420, 155) $comm_E = GUICtrlCreateEdit("", 15, 235, 410, 134) GUICtrlCreateGroup("", -99, -99, 1, 1) GUICtrlCreateGroup("Communication send", 10, 375, 420, 95) $send_E = GUICtrlCreateEdit("", 15, 390, 410, 74) GUICtrlCreateGroup("", -99, -99, 1, 1) $send_B = GUICtrlCreateButton("Send", 10, 475, 90, 25, $WS_GROUP) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ; Start The TCP Services ;============================================== TCPStartup() ; Initialize a variable to represent a server-connection ;============================================== Global $ConnectedSocket = -1 ; Initialize a variable to represent a client-connection ;============================================== Global $ConnectedToSocket = -1 Global $recv _main() Func _main() Local $nMsg While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $listen_B _listen() Case $connect_B _connect() Case $send_B If $ConnectedToSocket <> -1 Then _send() Else _FileWriteLog($logPath, 'You are not connected!') MsgBox(16, 'Error', 'You are not connected!', 5) EndIf EndSwitch If $ConnectedSocket <> -1 Then _receive() WEnd EndFunc ;==>_main Func _send() Local $txt = GUICtrlRead($send_E) If $txt <> '' Then TCPSend($ConnectedToSocket, $txt & @CRLF) ; If the send failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then _FileWriteLog($logPath, 'TCPSend error') Else GUICtrlSetData($comm_E, _ @UserName & " : " & $txt & @CRLF & GUICtrlRead($comm_E)) GUICtrlSetData($send_E, '') EndIf EndFunc ;==>_send Func _connect() ;Attempt to connect to SERVER at its IP and PORT ;======================================================= $ConnectedToSocket = TCPConnect(_GUICtrlIpAddress_Get($r_ip_IC), GUICtrlRead($r_port_I)) ; If there is an error... show it If @error Then MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error & @CRLF & 'Maybe nobody is listening ...') _FileWriteLog($logPath, 'Connecting error') EndIf EndFunc ;==>_connect Func _receive() ; Try to receive (up to) 2048 bytes ;---------------------------------------------------------------- $recv = TCPRecv($ConnectedSocket, 2048) ; If the receive failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then _FileWriteLog($logPath, 'Disconnected') $ConnectedSocket = -1 EndIf ; Update the edit control with what we have received ;---------------------------------------------------------------- If $recv <> "" Then GUICtrlSetData($comm_E, _ GUICtrlRead($r_hostname_I) & " -> " & $recv & @CRLF & GUICtrlRead($comm_E)) EndFunc ;==>_receive Func _listen() ; Create a Listening "SOCKET" ;============================================== Local $MainSocket = TCPListen(@IPAddress1, GUICtrlRead($l_port_I), 100) If $MainSocket = -1 Then _FileWriteLog($logPath, 'TCPListen error') Exit EndIf ;Wait for and Accept a connection ;============================================== Local $start = TimerInit() Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 Or (TimerDiff($start) > 25000) ; Get IP of client connecting Local $szIP_Accepted = SocketToIP($ConnectedSocket) GUICtrlSetData($r_hostname_I, _TCPIpToName($szIP_Accepted)) EndFunc ;==>_listen ; Function to return IP Address from a connected socket. ;---------------------------------------------------------------------- Func SocketToIP($SHOCKET) Local $sockaddr, $aRet $sockaddr = DllStructCreate("short;ushort;uint;char[8]") $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _ "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr)) If Not @error And $aRet[0] = 0 Then $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3)) If Not @error Then $aRet = $aRet[0] Else $aRet = 0 EndIf $sockaddr = 0 Return $aRet EndFunc ;==>SocketToIP Func _endscript() TCPCloseSocket($ConnectedSocket) TCPShutdown() Exit (0) EndFunc ;==>_endscript Mega Edited February 18, 2011 by Xenobiologist Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
jvanegmond Posted February 18, 2011 Share Posted February 18, 2011 Yeah, what Mega said: Add CRLF. $c_Ip = "127.0.0.1" $c_Port = 6879 TCPStartup() $socket = TCPConnect($c_Ip, $c_Port) if $socket = -1 Then msgbox(0, "Error", "Error Connecting") EndIf while True $msg = inputbox("Send msg", "Send msg to server") TCPSend($socket, $msg & @CRLF) ;<== Hereq sleep(1) WEnd github.com/jvanegmond Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now