许多语言都有Socket,可见Scoket有多流行。我对通信这方面还是很感兴趣的,但是无奈身边并没有这方面的高手。自己边学,边做了个这个聊天系统。。。还有许多漏洞了,大家指正吧。

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.Sockets;
using System.Net;
using System.Threading;
using System.IO;

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.Sockets;
using System.Net;
using System.Threading;
using System.IO;namespace Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} #region //定义变量
IPAddress HostIp = IPAddress.Parse("192.168.1.36");
IPEndPoint point;
Socket scoket;
bool falge = true;
Socket acceptScoket;
#endregion private void btnLoad_Click(object sender, EventArgs e)
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName()); HostIp = ieh.AddressList[0];
point = new IPEndPoint(HostIp, Int32.Parse("1600"));
scoket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//使scoket与本地终端相关联
scoket.Bind(point);
scoket.Listen(50);
acceptScoket = scoket.Accept(); //开启新的进程
Thread th = new Thread(new ThreadStart(Process));
th.Start();
} #region //声明委托
delegate void SetTextCallBack(string str); //定义回调函数
private void SetText(string str)
{
this.textBox2.AppendText(str + "/r/n");
} #endregion
#region //声明传送文件的委托
delegate void TranferFile(string message); //定义回调函数
private void fileTranfer(string message)
{
//string fileName = SaveAsDialog();
//FileStream file = File.Create(fileName);
//if (fileName != null)
//{
// Byte[] getBytes = new Byte[64];
// getBytes = Encoding.BigEndianUnicode.GetBytes(message.ToCharArray());
// file.Write(getBytes, getBytes.Length, 0);
// file.Close();
//}
//else
//{
// statusStrip1.Text = "没有选择文件";
//}
SaveFileDialog saDialog = new SaveFileDialog();
if (saDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saDialog.FileName;
FileStream file = File.Create(fileName);
int total =int.Parse(message.Substring(message.LastIndexOf('&') + 1, message.LastIndexOf('#')-message.LastIndexOf('&')-1));
while (total>0)
{
byte[] bytes = new byte[64];
acceptScoket.Receive(bytes,bytes.Length,0);
file.Write(bytes,0,bytes.Length);
total = total - bytes.Length;
}
file.Flush();
file.Close();
}
else
{
MessageBox.Show("没有选择文件");
}
}
#endregion //定义接收过程函数
private void Process()
{
if (acceptScoket.Connected)
{

while (falge)
{
byte[] info = new byte[64];
acceptScoket.Receive(info, info.Length, 0);
string message = Encoding.BigEndianUnicode.GetString(info);
if (message.Contains("&&"))
{
this.Invoke(new TranferFile(fileTranfer), new object[] { message });
}
else
{
this.Invoke(new SetTextCallBack(SetText), new object[] { message });
}
}
}
} private string SaveAsDialog()
{
SaveFileDialog saDialog = new SaveFileDialog();
if (saDialog.ShowDialog() == DialogResult.OK)
{
string result = saDialog.FileName;
return result;
}
else
{
return null;
}
} private void btnSend_Click(object sender, EventArgs e)
{
try
{
Byte[] sendBytes = new Byte[64];
string sendStr = this.textBox1.Text + ":" + this.textBox3.Text + "/r/n";
sendBytes = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
acceptScoket.Send(sendBytes, sendBytes.Length, 0);
}
catch
{ }
}
}
}


客户端的代码非常类似,只需要把侦听的代码改为scolet,connect();即可。

可以自己试一下。