C#原代码
最佳答案
**********服务器端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;//要引入的命名空间
namespace 服务器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("确实要关闭服务器吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (dr == DialogResult.No)
{
e.Cancel = true;
}
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)//最小到托盘
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)//还原窗体
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (this.textBox2.Text.Equals(""))
{
MessageBox.Show("确定不回复消息吗?", "提示");
}
if (this.textBox1.Text.Equals(""))
{
MessageBox.Show("请填写服务器的ip地址", "提示");
}
else
{
int port = Convert.ToInt32(this.comboBox1.SelectedItem);
string host =this.textBox1.Text;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(0);
this.textBox4.Text="waiting for guest!";
Socket temp = s.Accept();
this.textBox4.Text="building connection ...";
string recvstr = "";
byte[] recvbytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvbytes, recvbytes.Length, 0);//接收信息
recvstr += Encoding.ASCII.GetString(recvbytes, 0, bytes);//转换
this.textBox4.Text=recvstr;//显示传入的信息
string sendstr = this.textBox2.Text;//发送给客户的信息
byte[] bs = Encoding.ASCII.GetBytes(sendstr);//转换发送给客户的信息
temp.Send(bs, bs.Length, 0);//发送
temp.Close();//关闭连接
//Console.ReadLine();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedItem = Convert.ToString(2000);
}
private void button3_Click(object sender, EventArgs e)
{
//停止服务
}
private void button1_Click(object sender, EventArgs e)
{
string path;
this.openFileDialog1.ShowDialog();
path=this.openFileDialog1.FileName;
DirectoryInfo di = new DirectoryInfo(path);
this.textBox3.Text +=di.FullName;
}
}
}
*******************客户端
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.IO;
namespace 客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("确实要关闭吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (dr == DialogResult.No)
{
e.Cancel = true;
}
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Equals(""))
{
MessageBox.Show("请填写服务器的地址", "提示");
}
else if(this.textBox2.Text.Equals(""))
{
MessageBox.Show("发送消息不能为空!","提示");
}
else
{
try
{
int port = Convert.ToInt32(this.comboBox1.SelectedItem);
string host = this.textBox1.Text;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.textBox3.Text="connecting ...";
c.Connect(ipe);//连接到服务器
string sendstr = this.textBox2.Text;//发送的信息
byte[] bs = Encoding.ASCII.GetBytes(sendstr);//转换发送的信息
this.textBox3.Text="send messages";
c.Send(bs, bs.Length, 0);//发送
string recvstr = "";
byte[] recvbytes = new byte[1024];
int bytes;
bytes = c.Receive(recvbytes, recvbytes.Length, 0);//算出接收信息的长度
recvstr += Encoding.ASCII.GetString(recvbytes, 0, bytes);//转换成字
this.textBox3.Text=recvstr;
c.Close();//关闭连接
}
catch (ArgumentNullException e2)//拒绝时候的提示
{
Console.WriteLine("argumentnullexceptinon is {0}", e2);
}
catch (SocketException e3)
{
Console.WriteLine("socketexception is {0}", e3);
}
Console.WriteLine("press enter to exit");
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedItem = Convert.ToString(2000);
}
private void button1_Click(object sender, EventArgs e)
{
string pas;
this.openFileDialog1.ShowDialog();
pas = this.openFileDialog1.FileName;
FileInfo f = new FileInfo(pas);
this.textBox2.Text += f.FullName.ToString();
}
}
}