gpt4 book ai didi

c# - PHP - C# TCP 客户端/服务器如何发送字符串?

转载 作者:可可西里 更新时间:2023-11-01 02:42:34 28 4
gpt4 key购买 nike

嘿,我这里有一个 C# Tcp 服务器和一个 PHP 客户端都在工作。

服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ConsoleApplication2
{
class Program
{


public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
TcpListener myList = new TcpListener(ipAd, 8001);

myList.Start();

Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");


while (true)
{

Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

byte[] b = new byte[100];
int k = s.Receive(b);
string szReceived = Encoding.ASCII.GetString(b.Take(k).ToArray());
Console.WriteLine(szReceived);
Console.WriteLine("Recieved..." + szReceived + "---");

switch (szReceived)
{
case "ping":
Console.WriteLine("Ping wird ausgeführt!");
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("PONG !"));
s.Close();

break;

case "logout":

break;
}


}




/* clean up */

myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}



}



}
}

客户

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<?PHP

error_reporting(E_ALL);
ini_set("display_errors", 1);

$address="127.0.0.1";
$service_port = "8001";

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: <br>" .
socket_strerror(socket_last_error()) . "\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...<br>";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) <br>" .
socket_strerror(socket_last_error($socket)) . "\n";
}else {

echo "connected<br>";


}

$in = "ping";
$out = '';

echo "Client Sended: PING !<br>";

socket_write($socket, $in, strlen($in));
echo "SENDED!.\n<br>";

echo "Reading response:\n\n<br>";

while ($out = socket_read($socket, 2048)) {
echo $out;
}

socket_close($socket);

?>

我现在的问题是我现在如何调用我的 ping .. 然后发送 1 个或多个字符串并在我的服务器中捕获它们以控制它们或其他什么? ...

我之前已经在 VB 中做过这个,所以我知道我可能用 Streamreader 和 Write 做过,但我想这在这里不起作用?代码示例将非常有帮助

感谢每一个答案:)

最佳答案

你必须为自己定义一个协议(protocol)并用它来发送任何东西。例如发送这样的东西:

ping|string1,string2

表示ping命令,参数string1和string2。您可以定义满足您需求的任何内容。然后以这种格式发送它们,并在另一端解析。

这是一个基于您的代码的示例。看看这个(为了便于阅读,省略了一些未更改的代码部分):

....

Console.WriteLine(szReceived);
Console.WriteLine("Recieved..." + szReceived + "---");

var split = szReceived.Split('|');
if(split.Length==0)return;

var command = split[0];
var parameters = new string[0];

if (split.Length > 0)
parameters = split[1].Split(',');

switch (command)
{
case "ping":
Console.WriteLine("Ping wird ausgeführt!");
ASCIIEncoding asen = new ASCIIEncoding();

foreach (var p in parameters)
Console.WriteLine(p);

s.Send(asen.GetBytes("PONG !"));
s.Close();

break;

case "logout":

break;
}
....

PHP 代码:

...
$stringsToSend = array("Hello", "World!", "Sth");

$in = "ping|" . implode(",", $stringsToSend);
$out = '';

echo "Client Sended: PING !<br>";

socket_write($socket, $in, strlen($in));
echo "SENDED!.\n<br>";
...

关于c# - PHP - C# TCP 客户端/服务器如何发送字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32415925/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com