इंडी के TIdTCPServer घटक के साथ ऐसा करने से आपको कुछ भी नहीं रोक रहा है।
एक TIdTCPServer केवल कनेक्शन सेट अप करता है। आपको बाकी को लागू करने की आवश्यकता होगी। तो वास्तविक भेजने और प्राप्त करने का अनुक्रम जो भी आप चाहते हैं हो सकता है।
अपने TIdTCPServer घटक के OnExecute घटना में इस कोड डालें:
var
sName: String;
begin
// Send command to client immediately after connection
AContext.Connection.Socket.WriteLn('What is your name?');
// Receive response from client
sName := AContext.Connection.Socket.ReadLn;
// Send a response to the client
AContext.Connection.Socket.WriteLn('Hello, ' + sName + '.');
AContext.Connection.Socket.WriteLn('Would you like to play a game?');
// We're done with our session
AContext.Connection.Disconnect;
end;
करने का तरीका यहां सेटअप कर सकते हैं TIdTCPServer वास्तव में बस:
IdTCPServer1.Bindings.Clear;
IdTCPServer1.Bindings.Add.SetBinding('127.0.0.1', 8080);
IdTCPServer1.Active := True;
इस सर्वर बताता केवल लूपबैक पते पर सुनने के लिए, पोर्ट 8080 पर। यह आपके कंप्यूटर के बाहर से कनेक्ट होने से किसी को भी रोकता है।
फिर, अपने ग्राहक कनेक्ट करने के लिए, आप एक Windows कमांड प्रॉम्प्ट में जाकर निम्नलिखित लिख सकते हैं:
तुम्हारा नाम क्या है:
telnet 127.0.0.1 8080
यहाँ उत्पादन है?
मार्कस
हैलो, मार्कस।
क्या आप एक खेल खेलना चाहते हैं?
खोने वाले होस्ट के लिए कनेक्शन।
टेलनेट नहीं है? यहां बताया गया है कि install telnet client on Vista and 7 कैसे करें।
या एक TIdTCP क्लाइंट के साथ, आप ऐसा कर सकते हैं:
var
sPrompt: String;
sResponse: String;
begin
// Set port to connect to
IdTCPClient1.Port := 8080;
// Set host to connect to
IdTCPClient1.Host := '127.0.0.1';
// Now actually connect
IdTCPClient1.Connect;
// Read the prompt text from the server
sPrompt := IdTCPClient1.Socket.ReadLn;
// Show it to the user and ask the user to respond
sResponse := InputBox('Prompt', sPrompt, '');
// Send user's response back to server
IdTCPClient1.Socket.WriteLn(sResponse);
// Show the user the server's final message
ShowMessage(IdTCPClient1.Socket.AllData);
end;
यहाँ नोट करने के लिए एक महत्वपूर्ण बात यह है कि ReadLn बयान इंतजार डेटा उपलब्ध होने तक। यह सब के पीछे जादू है।
हो सकता है कि आप इस व्यवहार को 'IdTCPClient1.IOHandler.ReadLnWait' या' IdTCPClient1.IOHandler.WaitFor' विधियों के साथ अनुकरण कर सकें। अन्यथा, 'TIdCmdTCPCLient' का उपयोग करके आपकी समस्या हल हो सकती है। – LightBulb
इंडी का कौन सा संस्करण? –
वर्तमान में, मैं डेल्फी 2010 में इंडी 10 का उपयोग कर रहा हूं। – LightBulb