PHP
·
发表于 5年以前
·
阅读量:8301
ʹÓÃphp¶ÁÈ¡socket½Ó¿ÚµÄÊý¾Ý£¬Í¨¹ýphp´«µÝÇeÇo½¨ºÍÇeÇo²ÎÊý£¬µÃµ½*µ»Ø½a¹u
PHPÎļþ£º
<?php
class Test{
const IP='127.0.0.1';
const port=10003;
public static function main(){
header("Content-type:text/html;Charset=UTF-8");
$socket=new Test();
$result=$socket->connSocket("getmaillist\r\n{'id':2}\r\n");
echo $result;
}
private function connSocket($str){
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$res=@socket_connect($socket,self::IP,self::port);
if(!$res){
return;
}
socket_write($socket,$str);
$result="";
while($data = socket_read($socket,1024)){
$result.=$data;
}
socket_close($socket);
return $result;
}
}
Test::main();
JavaµÄSocket*þÎñ¶Ë£º
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(10003);
while (true) {
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
byte[] b = new byte[1024];
int len = is.read(b);
String inputString = new String(b, 0, len);
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
String result="";
//´¦Ài*¢À´µÄÊý¾Ý
if(inputString.contains("\r\n")){
String[] params=inputString.split("\r\n");
if(params[0].equals("getmaillist")){
String maillist=getmaillist();
result="ÇeÇo*½*¨£º"+params[0]+",ÇeÇo²ÎÊý£º"+params[1]+",ÇeÇo½a¹u£º"+maillist;
}else{
result="*Ç*¨²ÎÊý2";
}
}else{
result="*Ç*¨²ÎÊý1";
}
pw.println(result);
socket.close();
}
}
public static String getmaillist(){
return "ÖйuÉϺ£/2015Äe7ÔÂ28ÈÕ¡ª¡ª*þÎñÓÚÖйu¼°È«Ço»ªÈËÉçȺµÄÁiÏÈÔÚÏßýÌa¹«Ë¾ÐÂÀ˹«Ë¾(Nasdaq GS: SINA)¶¨ÓÚÃÀ¹uµ±µØʱ¼a2016Äe8ÔÂ8ÈÕÖÜÒ»¹ÉÊÐÊÕÅ̺o¹«²¼½ØÖÁ2016Äe6ÔÂ30ÈÕµÄ2016µÚ¶þ¼¾¶Èδ¾Éo¼ÆµÄ²ÆÎñ±¨¸ae¡£Ëaeºo£¬ÐÂÀ˹ÜÀiÍŶӽ«ÓÚÃÀ¹u¶«²¿Ê±¼aÍi10µa10*ÖÕÙ¿ªµç»°»aÒe£¬Í¨±¨¹«Ë¾µÄ²ÆÎñºÍ¾Óª×´¿o¡£";
}
}
Ч¹u£º
ÒÔÉÏÕaƪPHPÇeÇoSocket½Ó¿Ú²aÊÔʵÀý¾ÍÊÇС±a*ÖÏi¸ø´o¼ÒµÄÈ«²¿ÄÚÈÝÁË£¬Ï£ÍuÄܸø´o¼ÒÒ»¸o²Î¿¼£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£