Client端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket c;
public void Client(IPEndPoint serverIPEP)
{
try
{
c = new Socket(serverIPEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
c.Connect((EndPoint)serverIPEP);
MessageBox.Show("成功连接远程主机");
c.Send(System.Text.Encoding.Default.GetBytes("这是一个测试信息"));
byte[] data = new byte[1024];
int rect = c.Receive(data);
byte[] chat = new byte[rect];
Buffer.BlockCopy(data, 0, chat, 0, rect);
MessageBox.Show(System.Text.Encoding.Default.GetString(chat));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
Client(new IPEndPoint(IPAddress.Parse(this.IP.Text), int.Parse(this.Port.Text)));
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket c;
public void Client(IPEndPoint serverIPEP)
{
try
{
c = new Socket(serverIPEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
c.Connect((EndPoint)serverIPEP);
MessageBox.Show("成功连接远程主机");
c.Send(System.Text.Encoding.Default.GetBytes("这是一个测试信息"));
byte[] data = new byte[1024];
int rect = c.Receive(data);
byte[] chat = new byte[rect];
Buffer.BlockCopy(data, 0, chat, 0, rect);
MessageBox.Show(System.Text.Encoding.Default.GetString(chat));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
Client(new IPEndPoint(IPAddress.Parse(this.IP.Text), int.Parse(this.Port.Text)));
}
}
}
Server端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
Thread t;
//114.94.201.118
private void BeginListen()
{
IPHostEntry hostIP = Dns.GetHostByName(Dns.GetHostName());
IPAddress serverIP = hostIP.AddressList[0];
IPEndPoint iep = new IPEndPoint(serverIP, 2009);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage=new byte[1024];
socket.Bind(iep);
while (true)
{
socket.Listen(3);
Socket newSocket = socket.Accept();
try
{
string[] IP = newSocket.RemoteEndPoint.ToString().Split(':');
ListViewItem li = new ListViewItem();
li.SubItems.Clear();
li.SubItems[0].Text = IP[0];
li.SubItems.Add(IP[1]);
this.listView1.Items.Add(li);
newSocket.Receive(byteMessage);
string sTime = DateTime.Now.ToShortDateString();
string msg = sTime + ":" + " Message from :";
msg += newSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
this.textBox2.Text = msg;
newSocket.Send(byteMessage);
}
catch (Exception e)
{
MessageBox.Show("与远程主机失去连接");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(BeginListen));
t.Start();
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
Thread t;
//114.94.201.118
private void BeginListen()
{
IPHostEntry hostIP = Dns.GetHostByName(Dns.GetHostName());
IPAddress serverIP = hostIP.AddressList[0];
IPEndPoint iep = new IPEndPoint(serverIP, 2009);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage=new byte[1024];
socket.Bind(iep);
while (true)
{
socket.Listen(3);
Socket newSocket = socket.Accept();
try
{
string[] IP = newSocket.RemoteEndPoint.ToString().Split(':');
ListViewItem li = new ListViewItem();
li.SubItems.Clear();
li.SubItems[0].Text = IP[0];
li.SubItems.Add(IP[1]);
this.listView1.Items.Add(li);
newSocket.Receive(byteMessage);
string sTime = DateTime.Now.ToShortDateString();
string msg = sTime + ":" + " Message from :";
msg += newSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
this.textBox2.Text = msg;
newSocket.Send(byteMessage);
}
catch (Exception e)
{
MessageBox.Show("与远程主机失去连接");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(BeginListen));
t.Start();
}