TCP/IP 初探(转)
/*以下内容来自:http://www.luyikk.com/topic-11961.aspx
仅用于学习研究*/
很久以前就一直想玩P2P做一些商用的应用软件
早前没有C#,或者.NET的时候,我可以用VB6 采用 winsock来开发一些应用软件.
距离VB6那个时候已经有近 5年没有玩过了.
这几日开了VS2005来玩C#. 翻无数贴子,均不能成功实现C S 之间的通讯.
不是死机,就是到处有错.
经过俺几天几夜不眠不休的认真研究,最终研究得成正果,小小的爽了一把.
为了俺们大中华民族的未来软件产业的发展壮大,俺不敢私藏心得体会,
特此奉献如下, 有钱的打发几个小钱,没钱的捧个人场哈.大牛和大虾就当看猴戏撒.
废话少说,开场喽
---------------------------------------------------------------------------------------------------------------------------
我是独立二个项目来研究CS端,命名为Server 和 Client
如下图乱放:
上面这个图是S端的鸟样
这个是C端界面
//下面是代码部分:
-------------------------------------------------------------------------------------------------------------------
//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.IO;
using System.Net.Sockets;
using System.Threading;
namespace Client
{
public partial class Form1 : Form
{
//发送消息程序
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String mystr = textBox1.Text;
this.sendmymsg();
}
public void sendmymsg()
{
try
{
TcpClient client = new TcpClient("127.0.0.1", 5567);
NetworkStream sendStream = client.GetStream();
//StreamWriter writer = new StreamWriter(sendStream);
String msg = textBox1.Text;
Byte[] sendBytes = Encoding.Default.GetBytes(msg);
sendStream.Write(sendBytes, 0, sendBytes.Length);
//writer.Flush();
sendStream.Close();
client.Close();
textBox1.Text = "Msg Sent!";
}
catch(System.Exception e)
{
textBox2.Text = e.ToString(); ;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
//Server 部分比较复杂,也列出来,一会拿出来扯
//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.IO;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace Server
{
public partial class Form1 : Form
{
//变量
private ThreadStart start;
private Thread listenThread;
static private bool m_bListening = false;
static private System.Net.IPAddress MyIP = System.Net.IPAddress.Parse("127.0.0.1");
static private TcpListener listener = new TcpListener(MyIP, 5567);
private String msg;
//初始化监听器
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
start = new ThreadStart(startListen);
listenThread = new Thread(start);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (m_bListening)
{
m_bListening = false;
label4.Text = "Server Idle";
}
else
{
m_bListening = true;
listenThread.Start();
label4.Text = "Server Listening";
}
}
private void showmore()
{
textBox1.Text = msg;
}
private void startListen()
{
//textBox1.Text = "listening";
listener.Start();
//接收数据
while (m_bListening)
{
//测试是否有数据
try
{
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
//StreamReader sr = new StreamReader(ns);//流读写器
//字组处理
byte[] bytes = new byte[1024];
int bytesread = ns.Read(bytes, 0, bytes.Length);
msg = Encoding.Default.GetString(bytes, 0, bytesread);
//显示
//MessageBox.Show(msg);
showmore();
ns.Close();
client.Close();
//清场
}
catch (Exception re)
{
MessageBox.Show(re.Message);
}
}
listener.Stop();
//
}
}
}
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。