public bool networkbuild()//建立端口连接
{
if (client == null)
{
try
{
client = new TcpClient(networkip, networkport);
netstream = client.GetStream();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
return true;
}
public void networkclose()//关闭端口连接
{
if (client != null)
{
try
{
netstream.Close();
client.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public bool netsendmessage(byte[] message,int length)//发送数据
{
if (netconnected)
{
try
{
netstream.Write(message, 0, length);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
MessageBox.Show("连接已关闭");
return false;
}
public bool netreadmessage(byte[] message, int length)//读取数据
{
try
{
int i = 0;
int cont = 0;
if (netstream != null)
{
while (i<length)
{
cont = netstream.Read(message, i, length - i);
if (cont > 0)
{
i += cont;
}
}
}
else
{
return false;
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
相关文章
- 03-17转载(TCPClient 建立连接和断开连接函数)
- 03-17TCP连接的建立和断开
- 03-17TCP 连接建立和断开,以及状态转换
- 03-17TCP建立连接的三次握手和TCP连接断开的四次挥手
- 03-17TCP连接的建立和断开