Funkcionisanje Socket-a?

1

Funkcionisanje Socket-a?

offline
  • Pridružio: 29 Okt 2004
  • Poruke: 20

Pozdrav svima!

Imam nekakve primjere za Delphi, koji dolaze uz knjigu, a medji njima i kod za ClientSocket i ServerSocket.
Pitanje :
Da li Client moze da komunicira sa Server-om ako je Server na nekoj masini koja nije umrezena sa masinom na kojoj se nalazi Client, tacnije moze li se uspostaviti veza izmedju ova dva programa preko interneta?

Podesim oba programa na port 51 i IP adresu od kompa na kome je Server ali nece da uspostavi vezu, do cega je? Mozda je problem u portovima, neznam.
Nadam se da ce mi neko pomoci u ovome!

Hvala unapred!



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Pridružio: 18 Apr 2003
  • Poruke: 8134
  • Gde živiš: U kesici gumenih bombona...

mora i moze da se poveze... proveri firewall, mozda on pravi problem...



offline
  • Pridružio: 29 Okt 2004
  • Poruke: 20

Nisam bas upoznat sa tom problematikom,
sta je taj firewall, i kako da ga provjerim?
Da li se koristi port 51 za taj transfer informacija?

offline
  • Đuro Glumac
  • dipl. ing. informatike
  • Pridružio: 08 Feb 2004
  • Poruke: 3640
  • Gde živiš: ApAtIn

Da li koristis neku zastitu za upade u racunar...npr Zone Alarm ili sl.?
Kazes ne moze da se poveze, a kakvu gresku ti prijavljuje?

offline
  • Pridružio: 29 Okt 2004
  • Poruke: 20

Koristim Ad-aware 6.0, neznam, mozda nije dobar, ali nemam bolji.

Prijavljuje neku gresku "Asynchronus socket error 10060.",nisam siguran, tako nesto. Neznam do cega je.

offline
  • Pridružio: 18 Apr 2003
  • Poruke: 8134
  • Gde živiš: U kesici gumenih bombona...

jel ti je to prijavio preko Neta?
jel mozes da se konektujes u lokalu (preko mreze)?

offline
  • Pridružio: 26 Jun 2004
  • Poruke: 32
  • Gde živiš: Vranje

taj 10060 error znaci 'Connection timed out'... Znaci verovatno se konektujes 'u prazno', tj. nemas ispravan target.

Evo spisak gresaka, moze da ti koristi :

 case ErrorCode of     10004: Result := 'interrupted function call';     10013: Result := 'permission denied';     10014: Result := 'bad address';     10022: Result := 'invalid argument';     10024: Result := 'too many open files';     10035: Result := 'resource temporarily unavailable';     10036: Result := 'operation now in progress';     10037: Result := 'operation already in progress';     10038: Result := 'socket operation on non-socket';     10039: Result := 'destination address required';     10040: Result := 'message too long';     10041: Result := 'protocol wrong type for socket';     10042: Result := 'bad protocol option';     10043: Result := 'protocol not supported';     10044: Result := 'socket type not supported';     10045: Result := 'operation not supported';     10046: Result := 'protocol family not supported';     10047: Result := 'address family not supported by protocol family';     10048: Result := 'address already in use';     10049: Result := 'cannot assign requested address';     10050: Result := 'network is down';     10051: Result := 'network is unreachable';     10052: Result := 'network dropped connection on reset';     10053: Result := 'software caused connection abort';     10054: Result := 'connection reset by peer';     10055: Result := 'no buffer space available';     10056: Result := 'socket is already connected';     10057: Result := 'socket is not connected';     10058: Result := 'cannot send after socket shutdown';     10060: Result := 'connection timed out';     10061: Result := 'connection refused';     10064: Result := 'host is down';     10065: Result := 'no route to host';     10067: Result := 'too many processes';     10091: Result := 'network subsystem is unavailable';     10092: Result := 'winsock.dll version out of range';     10093: Result := 'successful wsastartup not yet performed';     10094: Result := 'graceful shutdown in progress';     11001: Result := 'host not found';     11002: Result := 'non-authoritative host not found';     11003: Result := 'this is a non-recoverable error';     11004: Result := 'valid name, no data record of requested type';   end;

offline
  • Pridružio: 29 Okt 2004
  • Poruke: 20

Program fino radi kad pokrenem kod sebe oba programa, medjutim kad povezujem programe preko interneta nece.

Mozda je glupo da pitam i ovo, sta je taj target, kako to utice na ovu vezu? Kako da podesim taj target da bi ova veza funkcionisala.

Neznam, ja sam jos pocetnik u ovome, nemojte da zamjerite ako moja pitanja nisu bas na pravom mestu, tj. ako nisu pravilno formulisanja.

Zahvaljujem na dosadasnjoj saradnji, ali nadam se da cu i ovaj problem rijesiti, tj, da mi vi pomognete u tome!
Danke!

offline
  • Pridružio: 26 Jun 2004
  • Poruke: 32
  • Gde živiš: Vranje

Pa 'target' ti je u stvari program na udaljenom racunaru na kome treba da se povezes. Ako ti prijavljuje 10060 gresku znaci da tvoj program ceka na odgovor, ali ne dobija ga... Bilo bi lepo kad bi okacio ovde source ako ti nije problem pa da vidimo.... I proveri da li ti firewall ne blokira port 51... (zasto bas koristis port 51 ?)

offline
  • Pridružio: 29 Okt 2004
  • Poruke: 20

//////////////////////////////////////////////////////
unit ServerForm;

interface

{$WARN UNIT_PLATFORM OFF}

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, FileCtrl, Sockets,WinSock;

const
wm_RefreshClients = wm_User;

type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
lbClients: TListBox;
Label1: TLabel;
Label2: TLabel;
lbLog: TListBox;
FileListBox1: TFileListBox;
Edit1: TEdit;
Label3: TLabel;
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
public
procedure RefreshClients (var Msg: TMessage);
message wm_RefreshClients;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add ('Connected: ' +
Socket.RemoteHost + ' (' +
Socket.RemoteAddress + ')' );
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add ('Disconnected: ' +
Socket.RemoteHost + ' (' +
Socket.RemoteAddress + ')' );
end;

procedure TForm1.RefreshClients;
begin
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strCommand, strFile, strFeedback: string;
begin
// read from the client
strCommand := Socket.ReceiveText;
lbLog.Items.Add ('Client: ' + Socket.RemoteAddress + ': ' +
strCommand);
// extract the file name (all commands have 5 characters)
strFile := Copy (strCommand, 6, Length (strCommand) - 5);

if Pos ('MESS!',strCommand) = 1 then
begin
ShowMessage(strFile);
end

// execute program
else if Pos ('EXEC!', strCommand) = 1 then
begin
if FileExists (strFile) and (
WinExec (pChar (strFile), sw_ShowNormal) > 31) then
strFeedback := 'ERROR' + strFile + ' activated'
else
strFeedback := 'ERROR' + strFile + ' not found';
Socket.SendText (strFeedback);
end

// send back a text file
else if Pos ('TEXT!', strCommand) = 1 then
begin
if FileExists (strFile) then
begin
strFeedback := 'TEXT!';
Socket.SendText (strFeedback);
Socket.SendStream (TFileStream.Create (
strFile, fmOpenRead or fmShareDenyWrite));
end
else
begin
strFeedback := 'ERROR' + strFile + ' not found';
Socket.SendText (strFeedback);
end;
end

// send back a bitmap file
else if Pos ('BITM!', strCommand) = 1 then
begin
if FileExists (strFile) then
begin
strFeedback := 'BITM!';
Socket.SendText (strFeedback);
Socket.SendStream (TFileStream.Create (
strFile, fmOpenRead or fmShareDenyWrite));
end
else
begin
strFeedback := 'ERROR' + strFile + ' not found';
Socket.SendText (strFeedback);
end;
end

// send back a directory listing
else if Pos ('LIST!', strCommand) = 1 then
begin
if DirectoryExists (strFile) then
begin
strFeedback := 'LIST!';
Socket.SendText (strFeedback);
FileListBox1.Directory := strFile;
Socket.SendText (FileListBox1.Items.Text);
end
else
begin
strFeedback := 'ERROR' + strFile + ' not found';
Socket.SendText (strFeedback);
end;
end
else
begin
strFeedback := 'ERROR' + 'Undefined command: ' + strCommand;
Socket.SendText (strFeedback);
end;

// log result
lbLog.Items.Add (strFeedback);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
wVersionRequested : WORD;
wsaData : TWSAData;
begin
{Start up WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
end;

procedure TForm1.FormShow(Sender: TObject);
var
p : PHostEnt;
s : array[0..128] of char;
p2 : pchar;
begin
{Get the computer name}
GetHostName(@s, 128-);
p := GetHostByName(@s);
Edit1.Text:=(p^.h_Name);
{Get the IpAddress}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
Edit1.Text:=p2;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
WSACleanup;
end;

end.


/////////////////////////////////////////////////////////////////////////////////////

unit ClientForm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp, ExtCtrls;

type
TCliStatus = (csIdle, csList, csBitmap, csText, csError);

TForm1 = class(TForm)
ClientSocket1: TClientSocket;
cbActivate: TCheckBox;
EditServer: TEdit;
Label4: TLabel;
btnExec: TButton;
EditServerFile: TEdit;
Label2: TLabel;
ListFiles: TListBox;
Label1: TLabel;
EditDir: TEdit;
btnGetDir: TButton;
LabelDir: TLabel;
Bevel1: TBevel;
btnBitmap: TButton;
btnText: TButton;
Button1: TButton;
Edit1: TEdit;
Label3: TLabel;
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure cbActivateClick(Sender: TObject);
procedure btnExecClick(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure btnGetDirClick(Sender: TObject);
procedure ListFilesClick(Sender: TObject);
procedure btnBitmapClick(Sender: TObject);
procedure btnTextClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
CliStatus: TCliStatus;
Buffer: array [0..9999] of Char;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

uses
CliBmp, CliText;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Caption := 'Connected';
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Caption := 'Disconnected';
end;

procedure TForm1.cbActivateClick(Sender: TObject);
begin
if not ClientSocket1.Active then
ClientSocket1.Address := EditServer.Text;
ClientSocket1.Active := cbActivate.Checked;
end;

procedure TForm1.btnExecClick(Sender: TObject);
begin
ClientSocket1.Socket.SendText ('EXEC!' + EditServerFile.Text);
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
strIn: string;
Stream: TMemoryStream;
nReceived: Integer;
begin
case CliStatus of
// look for data to receive
csIdle:
begin
Socket.ReceiveBuf (Buffer, 5);
strIn := Copy (Buffer, 1, 5);
if strIn = 'TEXT!' then
CliStatus := csText
else if strIn = 'BITM!' then
CliStatus := csBitmap
else if strIn = 'LIST!' then
CliStatus := csList
else if strIn = 'ERROR' then
CliStatus := csError;
end;
// show the messages (might actually not be an error)
csError:
begin
ShowMessage (Socket.ReceiveText);
cliStatus := csIdle;
end;
// get a directory listing
csList:
begin
ListFiles.Items.Text := Socket.ReceiveText;
cliStatus := csIdle;
end;
// read a text file
csText:
begin
with TFormText.Create (Application) do
begin
Memo1.Text := Socket.ReceiveText;
Show;
end;
cliStatus := csIdle;
end;
// read a bitmap file
csBitmap:
with TFormBmp.Create (Application) do
begin
Stream := TMemoryStream.Create;
Screen.Cursor := crHourglass;
try
while True do
begin
nReceived := Socket.ReceiveBuf (Buffer, sizeof (Buffer));
if nReceived <= 0 then
Break
else
Stream.Write (Buffer, nReceived);
// delay (200 milliseconds)
Sleep (200);
end;
// reset and load the temporary file
Stream.Position := 0;
Image1.Picture.Bitmap.LoadFromStream (Stream);
finally
Stream.Free;
Screen.Cursor := crDefault;
end;
Show;
cliStatus := csIdle;
end;
end; // case
end;

procedure TForm1.btnGetDirClick(Sender: TObject);
begin
ClientSocket1.Socket.SendText ('LIST!' + EditDir.Text);
LabelDir.Caption := EditDir.Text;
end;

procedure TForm1.ListFilesClick(Sender: TObject);
begin
EditServerFile.Text := LabelDir.Caption + '\' +
ListFiles.Items [ListFiles.ItemIndex];
end;

procedure TForm1.btnBitmapClick(Sender: TObject);
begin
ClientSocket1.Socket.SendText ('BITM!' + EditServerFile.Text);
end;

procedure TForm1.btnTextClick(Sender: TObject);
begin
ClientSocket1.Socket.SendText ('TEXT!' + EditServerFile.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
CliStatus := csIdle;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText('MESS!'+Edit1.Text);
end;

end.

Ko je trenutno na forumu
 

Ukupno su 806 korisnika na forumu :: 34 registrovanih, 5 sakrivenih i 767 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: A.R.Chafee.Jr., bojank, cavatina, Cobi026, Denaya, Fog of War, FOX, Frunze, ILGromovnik, Istman, JOntra, Kaplar2, Leonov, lord sir giga, Metanoja, mikrimaus, Nikolaa11, nizam, nobutado, novator, pceklic, pisske, repac, Skakac7, Smajser, Srle993, stegonosa, styg, theNedjeljko, Trpe Grozni, vladetije, W123, 125, 187