/serverSocket部分//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
namespace SocketListen
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Threading.Thread myThread;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button btnListen;
private System.Windows.Forms.Label label2;
private System.Net.Sockets.Socket mySocket;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
try
{
mySocket.Close();
myThread.Abort();
}
catch
{}
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.btnListen = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(112, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(152, 24);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 48);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(336, 136);
this.listBox1.TabIndex = 1;
//
// btnListen
//
this.btnListen.ForeColor = System.Drawing.Color.WhiteSmoke;
this.btnListen.Location = new System.Drawing.Point(272, 192);
this.btnListen.Name = "btnListen";
this.btnListen.TabIndex = 2;
this.btnListen.Text = "监听";
this.btnListen.Click += new System.EventHandler(this.btnListen_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 3;
this.label2.Text = "IP地址及端口";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Desktop;
this.ClientSize = new System.Drawing.Size(360, 229);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnListen);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "服务器监听消息";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
//得到服务器端IP地址
public static IPAddress getserverip()
{
System.Net.IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
return ieh.AddressList[0];
}
//开始监听
private void BeginListen()
{
//获取IP地址
System.Net.IPAddress serverip = getserverip();
//网络端点表示为IP地址和端口号
System.Net.IPEndPoint iep = new IPEndPoint(serverip,8000);
//实例化一个socket
mySocket = new Socket(System.Net.Sockets.AddressFamily.InterNetwork,SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
//获取的主机IP和端口号的字符串显示形式
this.label1.Text = iep.ToString();
//与本地IP地址绑定
mySocket.Bind(iep);
//得到客户端传送过来的字符数组
string[] strContent;
//传送文件内容的字节数组
byte[] byteContent;
//do
while(true)
{
try
{
//置于侦听状态
mySocket.Listen(5);
//为新建连接创建新的socket
System.Net.Sockets.Socket newSocket = mySocket.Accept();
//接收消息
newSocket.Receive(byteMessage);
strContent = System.Text.Encoding.Default.GetString(byteMessage).Split(new char[]{'|'});
byteContent = System.Text.Encoding.Default.GetBytes(strContent[1]);
///
FileStream fs = new FileStream("d://"+strContent[0],FileMode.Create,FileAccess.Write);
fs.Write(byteContent,0,byteContent.Length);
fs.Close();
//
//获取当前时间
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime +":message from:";
//消息接收时间,远程终结点和收到的发送自客户机的消息
msg += newSocket.RemoteEndPoint.ToString() + " :"+strContent[0];
this.listBox1.Items.Add(msg);
}
catch(SocketException ex)
{
this.label1.Text += ex.ToString();
}
}
//while(bytemessage != null)
}
//开始监听
private void btnListen_Click(object sender, System.EventArgs e)
{
try
{
//开始一个线程(监听)
myThread = new Thread(new ThreadStart(BeginListen));
myThread.Start();
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message,"完成",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
}
}
}
/clientSocket部分//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace SocketListenClient
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.TextBox txtip;
private System.Windows.Forms.TextBox txtport;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tboxFilePath;
private System.Windows.Forms.Button btnSel;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnSend = new System.Windows.Forms.Button();
this.txtip = new System.Windows.Forms.TextBox();
this.txtport = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label3 = new System.Windows.Forms.Label();
this.tboxFilePath = new System.Windows.Forms.TextBox();
this.btnSel = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(224, 240);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 0;
this.btnSend.Text = "发送";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// txtip
//
this.txtip.Location = new System.Drawing.Point(72, 16);
this.txtip.Name = "txtip";
this.txtip.Size = new System.Drawing.Size(216, 21);
this.txtip.TabIndex = 1;
this.txtip.Text = "";
//
// txtport
//
this.txtport.Location = new System.Drawing.Point(72, 48);
this.txtport.Name = "txtport";
this.txtport.Size = new System.Drawing.Size(216, 21);
this.txtport.TabIndex = 2;
this.txtport.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 23);
this.label1.TabIndex = 3;
this.label1.Text = "IP";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Port";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(248, 64);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Location = new System.Drawing.Point(24, 80);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(264, 88);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "消息";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 192);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 7;
this.label3.Text = "选择文件";
//
// tboxFilePath
//
this.tboxFilePath.Location = new System.Drawing.Point(72, 192);
this.tboxFilePath.Name = "tboxFilePath";
this.tboxFilePath.Size = new System.Drawing.Size(216, 21);
this.tboxFilePath.TabIndex = 8;
this.tboxFilePath.Text = "";
//
// btnSel
//
this.btnSel.Location = new System.Drawing.Point(136, 240);
this.btnSel.Name = "btnSel";
this.btnSel.TabIndex = 9;
this.btnSel.Text = "选择";
this.btnSel.Click += new System.EventHandler(this.btnSel_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.RosyBrown;
this.ClientSize = new System.Drawing.Size(304, 269);
this.Controls.Add(this.btnSel);
this.Controls.Add(this.tboxFilePath);
this.Controls.Add(this.label3);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtport);
this.Controls.Add(this.txtip);
this.Controls.Add(this.btnSend);
this.Name = "Form1";
this.Text = "客户机发送消息";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btnSend_Click(object sender, System.EventArgs e)
{
BeginSend();
}
//发送消息
private void BeginSend()
{
//得到客户机输入的IP地址
string ip = this.txtip.Text;
//得到客户机输入的端口号
string port = this.txtport.Text;
//得到传送的文件的内容的字符串
string strContent;
//将字符串转换为IP地址的实例
System.Net.IPAddress serverip = IPAddress.Parse(ip);
//转换字符串为INT型
int serverport = Convert.ToInt32(port);
//实例化网络端点(IP和端口)
System.Net.IPEndPoint iep = new IPEndPoint(serverip,serverport);
byte[] byteMessage;
byte[] bytePath;
byte[] byteTotalMessage;
//do
//实例化一个socket
Socket mySocket = new Socket(System.Net.Sockets.AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//连接指定的端点
mySocket.Connect(iep);
///
FileInfo finfo = new FileInfo(this.openFileDialog1.FileName);
bytePath = Encoding.ASCII.GetBytes(finfo.Name);
//
//用户自定义发送的消息
// byteMessage = Encoding.ASCII.GetBytes(this.textBox1.Text);
byteMessage = ReadFileByPath();
strContent = System.Text.Encoding.Default.GetString(byteMessage);
byteTotalMessage = System.Text.Encoding.Default.GetBytes(finfo.Name + "|" + strContent);
// //传送需发送文件的文件名称
// mySocket.Send(bytePath);
//客户机消息的发送
mySocket.Send(byteTotalMessage);
//禁用socket
mySocket.Shutdown(SocketShutdown.Both);
//关闭socket
mySocket.Close();
}
//选择要上传的文件
private void btnSel_Click(object sender, System.EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.tboxFilePath.Text = this.openFileDialog1.FileName;
}
}
//根据路径,将文件内容读取成字节数组
private byte[] ReadFileByPath()
{
FileStream fstream = new FileStream(this.openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
byte[] b = new byte[Convert.ToInt32(fstream.Length)];
fstream.Read(b,0,Convert.ToInt32(fstream.Length));
fstream.Close();
return b;
}
}
}