public class Network
{
private String server;
private Int32 port;
private TcpClient client;
private NetworkStream stream;
private Byte[] receiveBuff;
private Int32 receiveBuffSize;
private Byte[] sendBuff;
protected Boolean connected = false;
public Network(String server, Int32 port)
{
this.server = server;
this.port = port;
}
public void Connect()
{
if(!this.connected)
{
int attemp = 0;
try
{
if(attemp < 10)
{
this.client = new TcpClient();
this.client.Connect(this.server, this.port);
this.stream = this.client.GetStream();
this.connected = this.client.Connected;
}
}
catch(SocketException)
{
if(attemp > 10)
{
this.Disconnect();
}
attemp++;
}
}
}
public void Disconnect()
{
if(this.connected)
{
this.stream.Close();
this.client.Close();
this.connected = false;
}
}
public String Receive()
{
if(this.connected)
{
this.receiveBuffSize = this.client.ReceiveBufferSize;
this.receiveBuff = new Byte[this.receiveBuffSize];
this.stream.Read(receiveBuff, 0, this.receiveBuffSize);
return Encoding.UTF8.GetString(this.receiveBuff, 0, this.receiveBuffSize).Replace(" ","");
}
else
{
this.Disconnect();
return "";
}
}
public void Send(String msg)
{
if(this.connected)
{
this.sendBuff = Encoding.ASCII.GetBytes(msg);
this.stream.Write(this.sendBuff, 0, this.sendBuff.Length);
this.stream.Flush();
}
else
{
this.Disconnect();
}
}
}
Oto cała klasa poprawiona ale teraz mi się wiesz jak ktoś 2 raz przyciśnie connect i jak się rozłączy i znowu chce połączyć to się nic nie dzieje stoi jak by w miejscu.