Chat TCP, (ho sentito parlare di richieste chat a connessione diretta...)

« Older   Newer »
  Share  
Dragon[ITA]
CAT_IMG Posted on 10/3/2010, 21:09     +1   -1




codice:
SPOILER (click to view)
; Script Start - Add your code below here
#include <buttonconstants.au3>
#include <editconstants.au3>
#include <guiconstantsex.au3>
#include <guiipaddress.au3>
#include <progressconstants.au3>
#include <staticconstants.au3>
#include <windowsconstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Programmer Chat. by Dragon[ITA] *Andre*", 370, 391, 201, 129)
$IPAddress1 = _GUICtrlIpAddress_Create($Form1, 8, 364, 130, 21)
_GUICtrlIpAddress_Set($IPAddress1, "127.0.0.1")
$Port = GUICtrlCreateInput("3289", 152, 364, 41, 21)
$Label1 = GUICtrlCreateLabel("IP Address:", 8, 348, 58, 15)
$Label2 = GUICtrlCreateLabel("Port:", 152, 349, 26, 15)
$Username = GUICtrlCreateInput("Utente", 8, 324, 129, 21)
$Label = GUICtrlCreateLabel("Username", 8, 308, 52, 15)
$Button1 = GUICtrlCreateButton("Connetti", 288, 360, 73, 25, 0)
$Button2 = GUICtrlCreateButton("Crea Server", 200, 360, 73, 25, 0)
$Progress1 = GUICtrlCreateProgress(6, 285, 357, 20)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 369, 257, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_READONLY,$ES_WANTRETURN))
$Button3 = GUICtrlCreateButton("Clear", 144, 320, 65, 25, 0)
$Label3 = GUICtrlCreateLabel("N° Utenti Max", 224, 308, 70, 15)
$Users = GUICtrlCreateInput("2", 224, 324, 73, 21)
$Button4 = GUICtrlCreateButton("INFO", 304, 320, 57, 25, 0)
$Message = GUICtrlCreateInput("", 8, 261, 287, 21)
$Button5 = GUICtrlCreateButton("Invia", 304, 258, 57, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Dim $server = 0,$client = 0,$err = 0, $x = 1,$clear = 0
Dim $MainSocket, $szIP_Accepted, $ConnectedSocket[101]

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
If $server = 1 Then
For $x = 1 to GUICtrlRead($Users)
If $ConnectedSocket[$x] > 0 Then
TCPSend($ConnectedSocket[$x],"Server is going Offline ~~Bye")
EndIf
Next
EndIf
Exit

Case $Button1
If GUICtrlRead($button1) = "Connetti" Then
TCPShutdown()
GUICtrlSetData($progress1,10)
TCPStartup()
$ConnectedSocket[0] = -1
GUICtrlSetData($progress1,30)
$ConnectedSocket[0] = TCPConnect(_GUICtrlIpAddress_Get($IPAddress1),GUICtrlRead($Port))
GUICtrlSetData($progress1,100)
If @error Then
GUICtrlSetData($progress1,0)
MsgBox(4112, "Errore", "Connessione TCP fallita. Errore: " & @error & @CRLF & "Probabilmente il Server č offline")
Else
$ping = Ping(_GUICtrlIpAddress_Get($IPAddress1),1000)
GUICtrlSetData($progress1,$ping)
GUICtrlSetData($Button1,"Disconnetti")
GUICtrlSetData($Edit1,"Connesso con "&_GUICtrlIpAddress_Get($IPAddress1))
$err = 0
$server = 0
$client = 1
EndIf
Else
TCPShutdown()
GUICtrlSetData($progress1,0)
GUICtrlSetData($Edit1,"Disconnesso da "&_GUICtrlIpAddress_Get($IPAddress1))
GUICtrlSetData($Button1,"Connetti")
$err = 0
$server = 0
$client = 0
EndIf

Case $Button2
If GUICtrlRead($Button2) = "Crea Server" Then
TCPShutdown()
GUICtrlSetData($progress1,10)
TCPStartup()
GUICtrlSetData($progress1,30)
$MainSocket = TCPListen(_GUICtrlIpAddress_Get($IPAddress1),GUICtrlRead($Port))
GUICtrlSetData($progress1,100)
If $MainSocket = -1 Then
GUICtrlSetData($progress1,0)
MsgBox(16,"Errore","Errore nell'apertura sel socket."&@CRLF&"Probabilmente un Server č gia aperto su quella porta, o il tuo Firewall impedisce l'accesso.")
Else
GUICtrlSetData($progress1,0)
GUICtrlSetData($Button2,"Chiudi Server")
$u = GUICtrlRead($users)
GUICtrlDelete($Users)
$Users = GUICtrlCreateInput($u, 224, 324, 73, 21,$ES_READONLY)
GUICtrlSetData($Edit1,"~~Attendo gli Utenti")
$err = 0
$server = 1
$client = 0
For $x = 1 To GUICtrlRead($Users)
$ConnectedSocket[$x] = -1
Next
EndIf
Else
GUICtrlSetData($progress1,0)
For $x = 1 to GUICtrlRead($Users)
If $ConnectedSocket[$x] > 0 Then
GUICtrlSetData($progress1,GUICtrlRead($progress1)+100/$u)
TCPSend($ConnectedSocket[$x],"Server is going Offline ~~Bye")
TCPCloseSocket($ConnectedSocket[$x])
EndIf
Next
TCPShutdown()
GUICtrlSetData($progress1,100)
GUICtrlSetData($edit1, "Server Chiuso")
GUICtrlSetData($Button2,"Crea Server")
$u = GUICtrlRead($users)
GUICtrlDelete($Users)
$Users = GUICtrlCreateInput($u, 224, 324, 73, 21)
$err = 0
$server = 0
$client = 0
GUICtrlSetData($progress1,0)
EndIf

Case $Button3
GUICtrlSetData($edit1,"")

Case $Button4

Case $Users
If GUICtrlRead($users) > 100 Then
GUICtrlSetData($Users,"100")
EndIf

Case $Message
If $server = 1 Then
For $x = 1 to GUICtrlRead($Users)
If $ConnectedSocket[$x] > 0 Then
TCPSend($ConnectedSocket[$x],GUICtrlRead($Username)&" (server)>> "&GUICtrlRead($Message))
EndIf
Next
GUICtrlSetData($edit1, GUICtrlRead($Username)&" (server)>> "&GUICtrlRead($message)&@CRLF&GUICtrlRead($edit1))
GUICtrlSetData($message,"")
EndIf
If $client = 1 Then
TCPSend($ConnectedSocket[0],GUICtrlRead($Username)&">> "&GUICtrlRead($Message))
GUICtrlSetData($message,"")
EndIf
EndSwitch
$y = 0
For $x = 1 to GUICtrlRead($Users)
TCPSend($ConnectedSocket[$x],"")
If Not @error Then $y = $y + (100/$u)
Next
GUICtrlSetData($progress1,$y)

If $server = 1 Then
For $x = 1 To GUICtrlRead($Users)
If $ConnectedSocket[$x] < 0 Then
$ConnectedSocket[$x] = TCPAccept($MainSocket)
If $ConnectedSocket[$x] > 0 Then
$szIP_Accepted = SocketToIP($ConnectedSocket[$x])
GUICtrlSetData($Edit1,"Connesso con " &$szIP_Accepted& @CRLF &GUICtrlRead($edit1))
TCPSend($ConnectedSocket[$x], "~~Welcome"&@CRLF)
For $x = 1 to GUICtrlRead($Users)
If $ConnectedSocket[$x] > 0 Then
TCPSend($ConnectedSocket[$x],">> New Player: "&$szIP_Accepted&" <<")
EndIf
Next
EndIf
Else
$recv = TCPRecv($ConnectedSocket[$x],2024)
If @error Then
$szIP_Accepted = SocketToIP($ConnectedSocket[$x])
TCPCloseSocket($ConnectedSocket[$x])
$ConnectedSocket[$x] = -1
GUICtrlSetData($Edit1,$szIP_Accepted & " Disconnesso" & @CRLF & GUICtrlRead($edit1))
Else
If $recv <> "" Then
GUICtrlSetData($Edit1, $recv& @CRLF &GUICtrlRead($edit1))
For $x = 1 to GUICtrlRead($Users)
If $ConnectedSocket[$x] > 0 Then
TCPSend($ConnectedSocket[$x],$recv)
EndIf
Next
EndIf
EndIf
EndIf
Next
EndIf

If $client = 1 Then
$recv = TCPRecv($ConnectedSocket[0],2024)
If @error Then
GUICtrlSetData($Edit1,"Server Disconnesso"& @CRLF &GUICtrlRead($edit1))
GUICtrlSetData($Button1,"Connetti")
$err = 1
Else
If $recv <> "" Then
GUICtrlSetData($Edit1,$recv& @CRLF &GUICtrlRead($edit1) )
EndIf
EndIf
EndIf

If $err = 1 Then
$server = 0
$client = 0
$err = 0
EndIf

WEnd
For $x = 1 to GUICtrlRead($Users)
If $ConnectedSocket[$x] <> -1 Then TCPCloseSocket($ConnectedSocket[$x])
Next
TCPShutdown()



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


niente di speciale...
scrivi l'IP nell'apposita barra e crei un server con un minimo si 2 ed un massimo di 100 utenti in contemporanea.
(il programma funziona sia da server che da client.)
 
Top
Gianlu-thebest
CAT_IMG Posted on 10/3/2010, 21:43     +1   -1




Comunque bravo :) Se ti va guarda la versione 2 del mio media player ^_^
 
Top
Dragon[ITA]
CAT_IMG Posted on 10/3/2010, 22:36     +1   -1




grazie ma penso che la guarderņ domani ora vado a dormire... buonanotte
 
Top
Lahace
CAT_IMG Posted on 11/3/2010, 12:49     +1   -1




mmm riesce a connettersi a tutti i pc? anche con router?
 
Top
-Anubi-
CAT_IMG Posted on 11/3/2010, 14:03     +1   -1




Bella, anche se Anubi a causa di fastweb ( :( ) non la puņ provare
 
Top
4 replies since 10/3/2010, 21:09   186 views
  Share