ln.json/network/JSONTcpClient.cs

58 lines
977 B
C#

using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace sharp.json.network
{
public class JSONTcpClient
{
TcpClient client;
StreamReader reader;
StreamWriter writer;
JSONParser parser = new JSONParser();
public JSONTcpClient(int port)
{
this.client = new TcpClient();
this.client.Connect("127.0.0.1",port);
this.initialize();
}
public JSONTcpClient(TcpClient client)
{
this.client = client;
initialize();
}
private void initialize(){
this.reader = new StreamReader(client.GetStream());
this.writer = new StreamWriter(client.GetStream());
}
public JSON Receive()
{
string line = reader.ReadLine();
JSON json = parser.Parse(line);
return json;
}
public void Send(JSON json){
writer.WriteLine(json.ToString());
writer.Flush();
}
public void Close(){
this.client.Close();
}
public TcpClient TcpClient
{
get { return this.client; }
}
}
}