server c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Configuration;
using System.Net;
namespace server
{
class Program
{
static void Main(string[] args)
{
TcpListener tcpListener= new TcpListener(8200) ;
tcpListener.Start() ;
Console.WriteLine("Server Started") ;
Socket socketForClient ;
NetworkStream networkStream;
StreamReader streamreader;
socketForClient = tcpListener.AcceptSocket();
networkStream = new NetworkStream(socketForClient);
streamreader = new StreamReader(networkStream);
while(true)
{
if (socketForClient.Connected)
{
try
{
testmsg = streamreader.ReadLine();
}
catch
{
testmsg = null;
}
if (testmsg != null)
{
Console.WriteLine(testmsg);
}
else
{
streamreader.Close();
networkStream.Close();
socketForClient.Close();
}
}
else
{
socketForClient = tcpListener.AcceptSocket();
Console.WriteLine("new client connect");
networkStream = new NetworkStream(socketForClient);
streamreader = new StreamReader(networkStream);
}
}
}
}
}
client php
<?php
error_reporting(E_ALL);
set_time_limit(0);
echo "TCP/IP connection\n";
$service_port=8200;
$address="192.168.0.176";
$socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket<0){
echo "create socket error\n";
}
else
{
echo "create ok\n";
}
$result =socket_connect($socket,$address,$service_port);
if($result<0)
{
echo "connect failed\n".socket_strerror(socket_last_error());
}
else
{
echo "connect ok\n";
}
$in=date('Y-m-d');
if(!socket_write($socket,$in,strlen($in)))
{
echo "write failed\n";
}
else
{
echo "write success\n";
}
socket_close($socket);
echo "close ok";
?>