C# PHP – TCP socket

C# – ListenClient.class

class ListenClient

	private TcpListener tcpListener;
	private Socket clientSocket;

	public void ServerThreadProc(){
	   while(true){
		  try{
			clientSocket = tcpListener.AcceptSocket();
			 
			IPEndPoint clientInfo = (IPEndPoint)clientSocket.RemoteEndPoint;
			IPEndPoint serverInfo = (IPEndPoint)tcpListener.LocalEndpoint;
        //Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString() + " Connected");
			String clientAddress = clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString();
			
			try{
				if (clientSocket.Connected){
					int dataLength;
					byte[] bufferbytes = new byte[1000];

					dataLength = clientSocket.Receive(bufferbytes);

					String receiveData = Encoding.ASCII.GetString(bufferbytes, 0, dataLength);

					Console.WriteLine(receiveData);
					clientSocket.Send("reply something");
				}
				else{
					clientSocket.Close();
				}
				
			}
			catch(Exception se){
				clientSocket.Send(Encoding.ASCII.GetBytes("Connect failed."));
				clientSocket.Close();
				break;
			}
			catch(Exception ex){
				clientSocket.Close();
				break;
			}
		  }
		  catch(Exception ex){
			clientSocket.Close();
		  }
	   }

	}

}

C# – Server

IPAddress ip = IPAddress.Parse("127.0.0.1");
String Port = "1000";
TcpListener tcpListener = new TcpListener(ip, Int32.Parse(Port));
tcpListener.Start();

while(true){
   if (tcpListener.Pending()){
      ListenClient lc = new ListenClient(tcpListener);
      ThreadStart serverThreadStart = new ThreadStart(lc.ServerThreadProc);
      Thread t = new Thread(serverThreadStart);

      t.Start();
   }
   Thread.Sleep(100);
}

C# Client Send to C# Server

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//目標IP及PORT
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1000);
//連接到目標
s.Connect(endPoint);

byte[] byData = Encoding.ASCII.GetBytes("傳送內容");
s.Send(byData);

byte[] buffer = new byte[1024];
int iRx = s.Receive(buffer);
char[] chars = new char[iRx];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
//接收回應
String result = new String(chars);

s.Close();

C# Client Send to PHP Server

String url = "abc.php"; //目標PHP位置
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

req.Method = "POST";
string Data = "name=peter,age=20";//傳送的內容
byte[] postBytes = Encoding.ASCII.GetBytes(Data);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream resStream = response.GetResponseStream();

var sr = new StreamReader(response.GetResponseStream());
String responseText = sr.ReadToEnd(); //回傳的結果

PHP Client Send to C# Server

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$ip = "127.0.0.1";
$port = "1000";
$sendData = "傳送內容";

if($socket === false){
	return "socket create failed.";
}
else{
	$result = socket_connect($socket,$ip,$port);

	if($result === false){
		return "socket connect failed.";
	}
	else{
		$result = socket_write($socket,$sendData,strlen($sendData));

		if($result === false){
			return "socket write failed.";
		}
		else{
			//接收回傳結果
			$receivedData = socket_read($socket,2048);

			return $receivedData;
		}
	}
}

開始在上面輸入您的搜索詞,然後按回車進行搜索。按ESC取消。

返回頂部