C# Delphi与c之间的TCP通信问题#
我正在尝试使用TCP将文本行从c#客户端发送到delphi服务器。这两个行都只是在本地主机上运行。 我希望客户机在合适的时候发送文本行,服务器能够在合适的时候一行一行地从传入流中读取和处理它们 下面的代码通过delphi备忘录显示“文本1的某行”实现了这一点。 之后,c#返回异常,表示连接被强制关闭 如果每次发送一行文本时关闭客户端连接并重新建立一个新的连接,则可以达到预期效果。但这是非常缓慢的,不适合我的预期用途 我是TCP新手,不知道我在做什么!如果您能帮助我们达到预期的效果,我们将不胜感激 Delphi服务器代码是C# Delphi与c之间的TCP通信问题#,c#,delphi,sockets,tcp,C#,Delphi,Sockets,Tcp,我正在尝试使用TCP将文本行从c#客户端发送到delphi服务器。这两个行都只是在本地主机上运行。 我希望客户机在合适的时候发送文本行,服务器能够在合适的时候一行一行地从传入流中读取和处理它们 下面的代码通过delphi备忘录显示“文本1的某行”实现了这一点。 之后,c#返回异常,表示连接被强制关闭 如果每次发送一行文本时关闭客户端连接并重新建立一个新的连接,则可以达到预期效果。但这是非常缓慢的,不适合我的预期用途 我是TCP新手,不知道我在做什么!如果您能帮助我们达到预期的效果,我们将不胜感激
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Lines.Clear;
//Server initialization
TcpServer1 := TTcpServer.Create(Self);
TcpServer1.OnAccept := TcpServer1Accept;
TcpServer1.LocalPort := IntToStr(DEFAULT_PORT);
TcpServer1.Active := true;
end; //TForm1.FormCreate procedure ends
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
somestring : string;
begin
somestring := ClientSocket.Receiveln('#$D#$A');
Memo1.Lines.Add(somestring);
end; //TForm1.TcpServer1Accept ends
c#代码为
public static void Main (string[] args)
{
bool connectionEstablished = false;
int messageNum = 1;
TcpClient theclient = new TcpClient();
//first try establish a successful connection before proceeding
Console.WriteLine("Waiting for server......");
while (connectionEstablished == false) {
connectionEstablished = true;
try {
Int32 port = 2501;
string server = "127.0.0.1"; //the ip of localhost
theclient = new TcpClient(server, port);
} catch {
Console.WriteLine("Could not find AI server");
connectionEstablished = false;
}
} //while (connectionEstablished == false) ends
Console.WriteLine("Connected to server");
////////////////////////////////////////
while (true) {
try
{
string message = "some line of text " + messageNum.ToString() + "#$D#$A";
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = theclient.GetStream();
stream.Write(data, 0, data.Length); //working
messageNum = messageNum + 1;
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
System.Threading.Thread.Sleep(2500);
} //while (true) ends
////////////////////////////////////////////////
}
}
创建TcpClient实例后,实际上需要
连接
您还可以使用IPAddress a=IPAddress.Loopback;用于环回适配器,这样您就不需要解析它了
try
{
Int32 port = 2501;
string server = "127.0.0.1"; //the ip of localhost
theclient = new TcpClient();
theclient.Connect(server,port);
}
我不知道Delphi中socket类的确切行为,但在我看来,如果
OnAccept
procedureTForm1.TcpServer1Accept
结束,服务器就会关闭连接。因此,您还应该有一个循环,可能有一个结束标准:
procedure TForm1.TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient);
var
somestring : string;
begin
do
begin
somestring := ClientSocket.Receiveln('#$D#$A');
Memo1.Lines.Add(somestring);
end;
while(somestring.length > 0);
end; //TForm1.TcpServer1Accept ends
这将不是有效的Delphi代码,因为我已经很长时间没有使用Delphi/Pascal了,但我想你会明白…连接签名是
public void Connect(string hostname,int port)
或public void Connect(IPAddress address,int port)
,所以始终首先是ip/主机名,然后是端口,反之亦然。谢谢你的帮助。我尝试了这个方法,现在得到了一个异常“对已连接的套接字发出了连接请求”。connect()
只有在使用构造函数创建TcpClient而不包含端点信息时才有必要(如Paul Farry的示例中)。但是,当您使用服务器和端口创建TcpClient时,它将自动连接(“TcpClient构造函数(String,Int32):初始化TcpClient类的新实例并连接到指定主机上的指定端口。”)