using UnityEngine;
using System.Collections;
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading; public class sockectLogger : MonoBehaviour {
public static ArrayList outStr = new ArrayList();
public static string inStr = "";
public static TcpClient client = null;
public static NetworkStream stream = null; // Use this for initialization
void Start () {
SocketConnection("192.168.0.97", 1234);
} // Update is called once per frame
void Update () { }
public static void doLog(string str){
outStr.Add(str);
}
private void SocketConnection (string LocalIP, int LocalPort)
{
client = new TcpClient ();
try {
IPAddress ip = IPAddress.Parse (LocalIP);
IPEndPoint ipe = new IPEndPoint (ip, LocalPort); client.Connect (ipe);
Thread socketThread = new Thread (socketWork);
socketThread.Start ();
} catch (SocketException e) {
//ErrLog.RecordErr(e, ModuleName, "AsySocket", "");
log (e.ToString ());
} catch (Exception e) {
log ("ReceiveInt e=" + e.ToString ());
}
log ("=*> Socket thread start!");
}
public static void log (string str)
{
Debug.Log (str);
}
public static void socketWork ()
{
log ("=*> socketWork started v 5.0");
while (!client.Connected) {
log (".");
Thread.Sleep (300);
} Thread.Sleep (500);//
log ("=*> socketWork connected");
stream = client.GetStream ();
string tmp;
while (true) {
try {
if(outStr.Count>0){
tmp = outStr[0].ToString();
outStr.RemoveAt(0);
log (tmp);
Send (tmp);
inStr = ReceiveString();
log(inStr);
}
Thread.Sleep (10);
} catch (SocketException e) {
log ("socket thread exception 0:" + e.ToString ());
break;
} catch (Exception e) {
log ("socket thread exception 1:" + e.ToString ());
break;
}
}
log ("Socket thread end");
}
public static void Send (string data)
{
try {
byte[] _data = System.Text.Encoding.Default.GetBytes(data);
int len = _data.Length;
log ("Send -->*> " + data.Length + "/" + len);
stream.Write (_data, 0, data.Length);//,data.getPos(),SocketFlags.ControlDataTruncated);
} catch (SocketException e) {
log ("Send e=" + e.ToString ());
} catch (Exception e) {
log ("Send e=" + e.ToString ());
}
}
public static string ReceiveString ()
{
try {
byte[] recvBytes = new byte[512];
stream.Read (recvBytes, 0, 512);//从服务器端接受返回信息
//log ("receivestring recvBytes="+recvBytes.ToString());
string data = Encoding.UTF8.GetString (recvBytes);
return data;
} catch (SocketException e) {
log ("ReceiveString e=" + e.ToString ());
} catch (Exception e) {
log ("ReceiveString e=" + e.ToString ());
}
return "";
}
}

接受信息的服务器端:

<?php
ignore_user_abort(true);
set_time_limit(0);
ini_set('display_errors',true);
ini_set('session.use_cookies',false);
error_reporting(E_ALL);
$addr = "0.0.0.0";
$port = @ (int)$argv[1];
if($port<1024) $port = 1234;
define('EC',chr(1));
define('PORT',$port); $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
} if($socket < 0) {
echo "Socket create:".$socket_strerror($socket)."\n";
exit;
} if (! ($ret = socket_bind($socket, $addr, $port)) ) {
$cmd =isset($_SERVER['WINDIR'])? "netstat -an -o | find \"".$port."\"":"netstat -anp | grep \"".$port."\"";
$output = NULL;
exec( $cmd, $output , $return_var);
echo "
-----------------------------------------------
$cmd
<pre>";
var_dump($output, $return_var);
echo "/<pre>";
//echo "//AS!\n";
exit;
} if ( ($ret = socket_listen($socket, 5)) < 0 ) {
echo "socket listen:".socket_strerror()."\n";
exit;
} socket_set_nonblock($socket);
echo "Wainting for a connection at $port:\n";
$totalBytesRecv = 0;
$clients = array($socket);
while(true) {
$read = $clients;
$writes=NULL;$execs=NULL;
if(socket_select($read, $writes, $execs, 2) < 1){
echo ".";
continue;
} if(in_array($socket,$read)) {
$newsock = socket_accept($socket);
socket_set_nonblock($newsock);
socket_getpeername($newsock,$remoteIP,$remotePort);
$userSN = $remoteIP.'_'.$remotePort;
$clients[$userSN] = $newsock; $key = array_search($socket,$read);
unset($read[$key]);
echo $remoteIP.":".$remotePort." conected in.\r\n";
} foreach($read as $read_sock) {
$data = @socket_read($read_sock, 2048, PHP_BINARY_READ);
$data = trim($data);
if(strlen($data) == 2048){
socket_close($read_sock);
continue;
}
@socket_getpeername($read_sock, $remoteIP, $remotePort);
$userSN = $remoteIP.'_'.$remotePort; if($data == false) {
$key = array_search($read_sock, $clients);
unset($clients[$key]);
continue;
}
echo "\n".date("H:i:s").'->'.$data."\n";
$totalBytesRecv += strlen($data);
socket_write($read_sock,"OK".chr(0));
}
}
socket_close($socket);

一个socket发送调试信息的类的更多相关文章

  1. ZeroMQ接口函数之 :zmq_msg_send – 从一个socket发送一个消息帧

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_msg_send zmq_msg_send(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ ...

  2. C# Debug和Trace:输出调试信息

    在 C# 语言中允许在程序运行时输出程序的调试信息,类似于使用 Console.WriteLine 的方式向控制台输出信息.所谓调试信息是程序员在程序运行时需要获取的程序运行的过程,以便程序员更好地解 ...

  3. Windows程序调试系列: 使用VC++生成调试信息 转

    Windows程序调试系列: 使用VC++生成调试信息 ZhangTao,zhangtao.it@gmail.com, 译自 “Generating debug information with Vi ...

  4. ZeroMQ接口函数之 :zmq_sendmsg – 从一个socket上发送一个消息帧

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq-sendmsg zmq_sendmsg(3)        ØMQ Manual - ØMQ/4.1.0 Name ...

  5. ZeroMQ接口函数之 :zmq_send_const – 从一个socket上发送一个固定内存数据

    ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html ——————————————————————————————————— ...

  6. QObject提供了QMetaObject元类信息(相当于RTTI和反射),信号与连接,父子关系,调试信息,属性,事件,继承关系,窗口类型,线程属性,时间器,对象名称,国际化

    元类信息(相当于RTTI和反射),信号与连接,父子关系,调试信息,属性,事件,继承关系,窗口类型,线程属性,时间器,对象名称,国际化其中元类又提供了:classInfo,className,构造函数, ...

  7. js操作dom---创建一个域来输出调试信息

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. 一个用php实现的获取URL信息的类

    获取URL信息的类 使用这个类,你能获得URL的如下信息: - Host  - Path  - Statuscode (eg. 404,200, ...)  - HTTP Version  - Ser ...

  9. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...

随机推荐

  1. [转载] 对象存储(2):OpenStack Swift——概念、架构与规模部署

    原文: http://www.testlab.com.cn/Index/article/id/1085.html#rd?sukey=fc78a68049a14bb228cb2742bdec2b9498 ...

  2. bootstrap学习笔记<四>(table表格)

    表格 bootstrap为table表格定制多个常用样式:基本样式,隔行变色样式,带边框样式,荧光棒样式,紧凑样式,响应样式. ☑  .table:基础表格 ☑  .table-striped:斑马线 ...

  3. odoo关联表查询方法

    testinfo=self.env['product.attribute.value'].search([('product_ids.product_tmpl_id','=',2047)]) for ...

  4. ViewPager 基本方式加载view

    1.新建view_pager.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayo ...

  5. you don't have permission to view it 解决

    the file couldn't be opened because you don't have permission to view it   简单设置下面的选项即可,不要谢我啊! change ...

  6. Android开发面试经——2.常见Android基础笔试题

     标签: androidAndroid基础Android面试题Android笔试题 2015-03-12 15:04 3361人阅读 评论(3) 收藏 举报  分类: Android开发(29)  版 ...

  7. Python中的Class的讨论

    尽管Python在Function Programming中有着其他语言难以企及的的优势,但是我们也不要忘了Python也是一门OO语言哦.因此我们关注Python在FP上的优势的同时,还得了解一下P ...

  8. dom4j解析xml作为测试数据

    构造函数: public AppTestData(File xmlFile) throws Exception { file = xmlFile; try { saxReader = new SAXR ...

  9. 【bzoj1047】理想的正方形

    [bzoj1047]理想的正方形 题意 给定\(a*b\)由整数组成的矩形. 现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值 的差最小. \(1\leq a,b\leq 10 ...

  10. MySQL for Windows 解压缩版配置安装

    1.MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C: ...