客户端: 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.Sockets; using System.Net; using System.Threading; namespace Qiantai { public partial class Qiantai : Form { private System.Text.Encoding encode = System.Text.Encoding.GetEncoding("gb2312"); Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int nLianjieBiaozhi = 0; int nTingzhiFasong = 0; byte[] byRet = new byte[1]; int nXuhao = 10000; string str = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeefffff"; public delegate void ListBoxDelegate(string strTishi);
} public void XinxiFasong1() { int i; while (1 != nTingzhiFasong) { for (i = 0; i < 5; i++) { string strxinxi = strZifuchuan(nXuhao); byte[] bFasongXinxi = System.Text.Encoding.Default.GetBytes(strxinxi); try { sendsocket.Send(bFasongXinxi); } catch { } try { // 接收后台系统的响应 sendsocket.Receive(byRet); } catch { } if (!Convert.ToByte('S').Equals(byRet[0])) { MessageBox.Show("没有接到服务器端继续接受文件的响应"); return; } else { strXinxi = DateTime.Now.ToString() + " 第" + i.ToString() + "条信息发送成功"; ListBoxTishi(strXinxi); strXinxi = String.Empty; } } if (5 == i) { nTingzhiFasong = 1; } } } private void ListBoxTishi(string strTishiXinxi) { if (this.lsbXinxiTishi.InvokeRequired) { ListBoxDelegate LBDelegate = new ListBoxDelegate(ListBoxTishi); this.lsbXinxiTishi.Invoke(LBDelegate, new object[] { strTishiXinxi }); } else { this.lsbXinxiTishi.Items.Add(strTishiXinxi); this.lsbXinxiTishi.SelectedIndex = this.lsbXinxiTishi.Items.Count - 1; this.Refresh(); } } private void timer1_Tick(object sender, EventArgs e) { IPAddress IP = IPAddress.Parse(this.textBox2.Text); IPEndPoint ipep = new IPEndPoint(IP, 8001); do { nLianjieBiaozhi = 0; try { // 建立连接 sendsocket.Connect(ipep); } catch { nLianjieBiaozhi = 1; } } while (nLianjieBiaozhi == 1); Thread thread; for (int i = 1; i <= 100; i++) { thread = new Thread(new ThreadStart(XinxiFasong1)); thread.Name = "thread" + i.ToString(); thread.Start();
}
timer1.Stop(); } } }
服务器端:启动就可以运行的,服务器可以接收多个客户端连接,这里我只开了2个线程,客户端,开了100个线程循环发送信息到服务器,服务器端添加 一个listbox 客户端添加一个textbox 里面填写服务器的IP 和一个listbox ,listbox里面会显示运行信息,方便观察效果 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.Sockets; using System.Net; using System.Threading; using System.IO; using System.Data.SqlClient;namespace Houtai { public partial class Form1 : Form { public delegate void ListBoxDelegate(string strTishi); private System.Text.Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
你所说的问题:关于类工程,项目,代码放哪里这些问题,根本就与socket没有任何关系,从另外的角度来说无论你是socket还是其它的比如SerialPort之类的编程都与类工程,项目,代码没有关系。我建议你先试着做一些小软件,不要急于弄清楚所有的问题,一步步来。以我的观点来说,你要弄清楚类工程,项目,代码的位置这些确实是很基础的,那么你就可以把socket放一边,不考虑socket.
1:类工程与窗口应用程序工程的区别。
这里我不谈ASP.NET,只谈类库工程与应用程序(Console或者Windows Form)。
首先谈谈代码上的区别。他们之间在编写绝大部分代码的时候没有任何区别,类库的工程也可以添加窗体、UserControl等,而应用程序则必须有一个静态的Main函数作为整个应用程序的入口点。类库也可以有这个函数,绝对没有问题;但应用程序没有这个函数就会有问题。换一句话说,一个应用程序完全可以编译为一个类库,而反过来则未必成立。
再次从编译结果来看,应用程序肯定是一个exe文件而类库则编译成一个dll文件;它们之间的这种区别就在于dll无法直接执行,你可以这样来理解,编译为dll的代码或者工程它实际上是给其它应用程序提供一些类或者说API的。之所以分为exe和dll甚至一个应用程序需要多个dll(这很常见)是因为他们各自完成独立的功能,可以分别的开发、发布和管理。单纯的从功能的角度来说,一个应用程序可以一个dll也不要把所有的代码都编译进application。
比如:在一个大的应用程序中,我可以把与用户管理、角色管理、授权管理等有关的类,代码都放到一个类库中编译,比如:security.dll。然后把这部分工作交给某一个人或者一个小组来进行开发,只要接口不变化,这个dll可以自己独立的开发、发布新版本,更新等而不影响其它应用功能的工作。同时对于不需要这部分功能的应用程序来说我就不复制(引用)这个文件,减少发布的文件数量。2:你说的代码位置的问题。
基本上在C#中只有命名空间的概念,在文件中的物理位置是没有关系的。
也就是说你要使用一个类,只要它在你所引用的命名空间内就可以,不管它在哪个文件中都能找得到。至于项目中建立的各种文件夹无论是你自己建立的还是开发环境自动建立的,都不影响我上面说的这个命名空间的原则。
它的影响在于你开发时,例如,你建立了一个工程,默认命名空间为Project,然后又在工程中添加了一个文件夹为A。如果你在这个工程中直接添加一个类(.cs)文件,那么你会发现这个类的命名空间为Project,如果你在A文件夹下添加一个类(class),默认情况下这个类的命名空间将位于Project.A。就这样吧……
用UDP和TCP两种连接方式.
编程上, 远程数据库连接通常不会考虑连接端口号,连接方式,协议等, 这些细节VS一般已经在内部封装好了.
但Socket通信不同, 你要很仔细地规划用什么端口号发送消息, 什么端口号侦听消息,使用TCP还是UDP连接.
并且SOCKET发送的数据是一个数据流, 你必须先将发送的数据按一定规范转换为这种形式, 接收端则要将收到的数据按一定规范还原为原来的格式.
SOCKET通讯要比vs SQL编程底层一些, 所以才要封装成一个类, 让它尽可能象 VS SQL编程一样, 将复杂的东西变得简单.
http://topic.csdn.net/u/20081111/09/b820a433-a285-4c26-8aa6-dc0864f54c55.html
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.Sockets;
using System.Net;
using System.Threading;
namespace Qiantai
{
public partial class Qiantai : Form
{
private System.Text.Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int nLianjieBiaozhi = 0;
int nTingzhiFasong = 0;
byte[] byRet = new byte[1];
int nXuhao = 10000;
string str = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeefffff";
public delegate void ListBoxDelegate(string strTishi);
string strXinxi = string.Empty;
public Qiantai()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Start(); }
public string strZifuchuan(int nXuliehao)
{
string strChuan = nXuliehao.ToString() + str;
nXuhao++;
return strChuan;
}
public void XinxiFasong1()
{
int i;
while (1 != nTingzhiFasong)
{
for (i = 0; i < 5; i++)
{
string strxinxi = strZifuchuan(nXuhao);
byte[] bFasongXinxi = System.Text.Encoding.Default.GetBytes(strxinxi); try
{
sendsocket.Send(bFasongXinxi);
}
catch
{
} try
{
// 接收后台系统的响应
sendsocket.Receive(byRet);
}
catch
{ } if (!Convert.ToByte('S').Equals(byRet[0]))
{
MessageBox.Show("没有接到服务器端继续接受文件的响应");
return;
} else
{
strXinxi = DateTime.Now.ToString() + " 第" + i.ToString() + "条信息发送成功";
ListBoxTishi(strXinxi);
strXinxi = String.Empty;
}
}
if (5 == i)
{
nTingzhiFasong = 1;
}
}
} private void ListBoxTishi(string strTishiXinxi)
{
if (this.lsbXinxiTishi.InvokeRequired)
{
ListBoxDelegate LBDelegate = new ListBoxDelegate(ListBoxTishi);
this.lsbXinxiTishi.Invoke(LBDelegate, new object[] { strTishiXinxi });
}
else
{
this.lsbXinxiTishi.Items.Add(strTishiXinxi);
this.lsbXinxiTishi.SelectedIndex = this.lsbXinxiTishi.Items.Count - 1;
this.Refresh();
} } private void timer1_Tick(object sender, EventArgs e)
{
IPAddress IP = IPAddress.Parse(this.textBox2.Text);
IPEndPoint ipep = new IPEndPoint(IP, 8001);
do
{
nLianjieBiaozhi = 0;
try
{
// 建立连接
sendsocket.Connect(ipep);
}
catch
{
nLianjieBiaozhi = 1;
}
}
while (nLianjieBiaozhi == 1);
Thread thread;
for (int i = 1; i <= 100; i++)
{
thread = new Thread(new ThreadStart(XinxiFasong1));
thread.Name = "thread" + i.ToString();
thread.Start();
}
timer1.Stop();
}
}
}
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.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Data.SqlClient;namespace Houtai
{
public partial class Form1 : Form
{
public delegate void ListBoxDelegate(string strTishi);
private System.Text.Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
Socket receivesocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string hostname = Dns.GetHostName();
string strXinxi = string.Empty;
string strXinxi1 = string.Empty;
string strCharuXinxi = string.Empty;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Start(); } private void timer1_Tick(object sender, EventArgs e)
{
IPHostEntry iphostendpoint = Dns.GetHostEntry(hostname);
IPAddress[] address = iphostendpoint.AddressList;
IPEndPoint ipep = new IPEndPoint(address[0], 8001); //建立远程计算机的的socket
receivesocket.Bind(ipep);
receivesocket.Listen(0);
Thread thread, thread1;
thread = new Thread(new ThreadStart(Tongxun));
thread1 = new Thread(new ThreadStart(Tongxun1));
thread.Start();
thread1.Start();
timer1.Stop();
}
private void Tongxun()
{
Socket hostsocket = null;
while (true)
{
byte[] bJieshouXinxi = new byte[60];
hostsocket = receivesocket.Accept();
for (int i = 0; i < 500; i++)
{
try
{
hostsocket.Receive(bJieshouXinxi);
}
catch
{
break;
} strXinxi = "第1个线程已经启动";
ListBoxTishi(strXinxi);
strXinxi = String.Empty;
strCharuXinxi = encode.GetString(bJieshouXinxi);
strXinxi = DateTime.Now.ToString() + " " + encode.GetString(bJieshouXinxi);
ListBoxTishi(strXinxi);
strXinxi = String.Empty;
//Thread.Sleep(1000); try
{
// 接收完头文件发送响应消息
hostsocket.Send(new byte[] { Convert.ToByte('S') });
}
catch
{
break;
} }
}
}
private void Tongxun1()
{
Socket hostsocket = null;
while (true)
{
byte[] bJieshouXinxi = new byte[60];
hostsocket = receivesocket.Accept();
for (int i = 0; i < 500; i++)
{
try
{
hostsocket.Receive(bJieshouXinxi);
}
catch
{
break;
} strXinxi1 = "第2个线程已经启动";
ListBoxTishi(strXinxi1);
strXinxi1 = String.Empty; strXinxi1 = DateTime.Now.ToString() + " " + encode.GetString(bJieshouXinxi);
ListBoxTishi(strXinxi1);
strXinxi1 = String.Empty;
//Thread.Sleep(1000);
try
{
// 接收完头文件发送响应消息
hostsocket.Send(new byte[] { Convert.ToByte('S') });
}
catch
{
break;
}
}
}
}
private void ListBoxTishi(string strTishiXinxi)
{
if (this.lsbXinxiTishi.InvokeRequired)
{
ListBoxDelegate LBDelegate = new ListBoxDelegate(ListBoxTishi);
this.lsbXinxiTishi.Invoke(LBDelegate, new object[] { strTishiXinxi });
}
else
{
this.lsbXinxiTishi.Items.Add(strTishiXinxi);
this.lsbXinxiTishi.SelectedIndex = this.lsbXinxiTishi.Items.Count - 1;
this.Refresh();
} } private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
}