static void Main(string[] args) { TcpClientSession clientSession= new AsyncTcpSession(new DnsEndPoint("127.0.0.1", 55555)); clientSession.Connect(); clientSession.Connected += ClientSession_Connected; clientSession.Closed += ClientSession_Closed; clientSession.Error += ClientSession_Error; clientSession.DataReceived += ClientSession_DataReceived; while (true) { if (clientSession.IsConnected) { break; } Thread.Sleep(1000); } var data=BlockWriter.GetBytes("abcdefg"); while (true) { clientSession.Send(data, 0, data.Length); Interlocked.Increment(ref _sendedBlockCount); } Console.ReadKey(); }
public NonBlockingConnection(string host, int port, int receiveBufferSize, ISystemHandler handler) { this.hostname = host; this.port = port; this.receiveBufferSize = receiveBufferSize; this.handler = handler; this.eHandler = new InnerEventHandler(handler, this); // IP解析 EndPoint e = new IPEndPoint(IPAddress.Parse(host), port); session = new AsyncTcpSession(e, receiveBufferSize); // 注册回调事件 session.Error += this.eHandler.ConnException; session.Closed += this.eHandler.ConnDisConnected; session.Connected += this.eHandler.ConnConnected; session.DataReceived += this.eHandler.DataReceive; }
public TransferSocket(EndPoint endpoint, string fileName, string saveName) { readBuffer = new byte[PacketSize]; _fileName = fileName; _saveName = saveName; var cmds = new UpLoadSocketCommandBase[] { new CheckFile(), new DoData(), new Text(), }; foreach (var item in cmds) m_CommandDict.Add(item.Name, item); TcpClientSession client = new AsyncTcpSession(endpoint); client.Error += client_Error; client.DataReceived += client_DataReceived; client.Connected += client_Connected; client.Closed += client_Closed; Client = client; CommandReader = new TransferReader(this); }
void OnGUI() { if (GUI.Button (new Rect(100, 130, 100, 25), "SuperSocket")) { buffer.Data = new byte[8192]; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(host), port); AsyncTcpSession client = new AsyncTcpSession(endPoint); client.Connected += OnConnected; client.DataReceived += OnDataReceive; client.Connect(); NamePacket.Builder b = new NamePacket.Builder(); b.SetName("Client name data"); NamePacket np = b.BuildPartial(); MemoryStream stream = new MemoryStream(); np.WriteTo(stream); byte[] bytes = stream.ToArray(); BasePacket.Builder basePacketBuilder = new BasePacket.Builder(); basePacketBuilder.SetPacketId(64553060); basePacketBuilder.SetPacketData( ByteString.CopyFrom(bytes) ); BasePacket basePacket = basePacketBuilder.BuildPartial(); sendMessage(client, basePacket); print("SuperSocket!"); } }
public static void sendMessage(AsyncTcpSession client, BasePacket packet) { // using (MemoryStream sendStream = new MemoryStream()) { // CodedOutputStream os = CodedOutputStream.CreateInstance(sendStream); // WriteMessageNoTag equivalent to WriteVarint32, WriteByte (byte []) // that is: variable length message header + message body // os.WriteMessageNoTag(request); // os.WriteRawBytes(bytes); // os.Flush(); MemoryStream sendStream = new MemoryStream(); packet.WriteTo(sendStream); byte[] bytes = sendStream.ToArray(); client.Send(new ArraySegment<byte>(bytes)); // } }