Autor Beitrag
Eddy14
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 26



BeitragVerfasst: Do 30.11.06 22:09 
Hallo, da ich zurzeit ein Multiplayer Spiel mit den Tserver/TClientsockets programmiere, würde ich es gerne vermeiden das mein Programm 2mal gestartet wird um mit 2 Accounts zu spielen. Also hab ich mir gedacht ich schau einfach ob mein Port schon benutzt wird. Wenn ja, heißt das mein Programm ist schon einmal gestartet worden.

Also: Kann ich irgendwie feststellen ob ein Port benutzt wird?

PS: Bei den Server/Clientsockets kann man irgendwie einen Port 2 oder mehrfach verwenden ohne jegliche error. Die Suche hab ich auch schon verwendet...
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Do 30.11.06 22:38 
Naja, du kannst mehrere Clientverbindungen auf einem PC zu anderen PCs über einen Port laufen lassen, aber es kann an einem PC nur ein Server auf einem Port lauschen.

Daher könntest du einfach mal nen Server auf dem gewünschten Port auf active stellen und wenn er dir ne entsprechende Fehlermeldung ausspuckt, weißt du, dass der Port bereits belegt ist.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 30.11.06 23:06 
Moin!

Verhindere doch einfach, dass die Anwendung ein weiteres mal gestartet werden kann. Geht z.B. mit einem Suche in: Delphi-Forum, Delphi-Library MUTEX, alternative Suchbegriffe: Suche in: Delphi-Forum, Delphi-Library MEHRFACHSTART
Ist IMHO sicher besser, als das portbasiert zu machen. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Do 30.11.06 23:13 
Bei nem Mutex hast du halt das Problem, dass der weiterexistiert, auch wenn deine Anwendung abstürtzt.
N Porttest kann eh nicht schaden, es könnte ja der dumme Zufall sein, dass da schon ein anderes Programm drauf lauscht. Aber klar, um mehrere Instanzen zu verhindern, gibt es wirksamere Methoden, Narses hat den entsprechenden Hinweis ja schon gegeben.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 30.11.06 23:15 
Moin!

user profile iconalias5000 hat folgendes geschrieben:
Bei nem Mutex hast du halt das Problem, dass der weiterexistiert, auch wenn deine Anwendung abstürtzt.

try-finally... 8) sag ich nur. ;)

user profile iconalias5000 hat folgendes geschrieben:
N Porttest kann eh nicht schaden, es könnte ja der dumme Zufall sein, dass da schon ein anderes Programm drauf lauscht.

Hatte auch erst "überlesen", dass er den Client(!) meint! Aber man kann eh nicht wirklich verhindern, dass einer zwei Accounts gleichzeitig nutzt (-> 2. Rechner...). :|

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Do 30.11.06 23:29 
user profile iconNarses hat folgendes geschrieben:
user profile iconalias5000 hat folgendes geschrieben:
Bei nem Mutex hast du halt das Problem, dass der weiterexistiert, auch wenn deine Anwendung abstürtzt.

try-finally... 8) sag ich nur. ;)


Hm? Wie willst du das dann genau lösen? Um zu jedem Zeitpunkt, an dem das Programm läuft, über Mutexe den Spaß feststellen zu können, muss ich doch ständig nen Mutex laufen haben. Und ich kann ja nich des ganze programm in try...finally setzen...

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 30.11.06 23:32 
Moin!

So, z.B.: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
try
  Application.Run;
finally
  // Mutex freigeben
end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Do 30.11.06 23:35 
hm, ja müsst ich mal ausprobieren :lol:

jetzt denk ich aber mal, dass ein halbes back to topic drinnen wäre (ganz warn wir ja noch net vom Topic weg ;) )

_________________
Programmers never die, they just GOSUB without RETURN
Eddy14 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 26



BeitragVerfasst: Fr 01.12.06 16:54 
Wow, danke für die Antworten. Habs gelöst mit mutex.
Naja, wenigstens werde ich meinen Bruder davon abhalten mit 2 accounts gegen mich zu spielen :P

So siehts nun bei mir aus :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
  mHandle := CreateMutex(nil, True, 'Cold-War Onlinegame');
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
    ShowMessage('Nana, versuch nicht zu schummeln ^^');
    Application.Terminate();
  end;


Danke euch :)
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 01.12.06 17:52 
user profile iconNarses hat folgendes geschrieben:
Moin!

user profile iconalias5000 hat folgendes geschrieben:
Bei nem Mutex hast du halt das Problem, dass der weiterexistiert, auch wenn deine Anwendung abstürtzt.

try-finally... 8) sag ich nur. ;)


Es gibt so böse Fehler, die lassen die Applikation sang & klanglos abstürzen, da hilft auch kein try/finally. Was, wenn ein Programm "hängt", und man muss es per Taskmanager killen?

Die bessere Lösung ist die Verwendung von Semaphores, die werden sogar bei brutalem Abschiessen vom Betriebssystem wieder weggeräumt.

Edit: Hier Beispielcode:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
//Semaphore erzeugen, um Mehrfachstart zur gleichen Zeit zu unterbinden
FSemaphore := CreateSemaphore(nil11, PChar('MEINPROGRAMM'));

//Prüfen ob die Semaphore bereits existierte
if (GetLastError = ERROR_ALREADY_EXISTS) then
  raise Exception.Create('Semaphore existiert bereits!');


Cu,
Udontknow