using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace NetClient
{
public partial class SocketClient : Form
{
Socket s;
int count = 0;//计数器,若连接远程主机后,远程主机关闭,则接受的信息就为空,当10次为空后自动断开连接
IPEndPoint ipEndPoint;
Thread thread;
public SocketClient()
{
InitializeComponent();
}
private void SocketClient_Load(object sender, EventArgs e)
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
this.textBox1.Text = ip[0].ToString();//默认的主机IP
}
/// <summary>
/// 连接远程主机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip;//主机IP
int port;//主机端口号
try
{
ip = IPAddress.Parse(this.textBox1.Text.Trim());//远程主机IP
}
catch (Exception ex)
{
MessageBox.Show("IP地址错误!");
this.textBox1.Focus();
return;
}
try
{
port = Convert.ToInt32(this.textBox2.Text.Trim());//远程主机端口号
}
catch (Exception ex)