- 浏览: 18975 次
最新评论
简单的C#Socket编程
2009年06月23日
using System;
using System.Net;
using System.Net.Sockets;
using System.IO ;
public class Echoserver
{
//entry point of main method.
public static void Main()
{
//TcpListener is listening on the given port
Int32 port = 1234;
//IPAddress is connetct ip address
//IPAddress addr = IPAddress.Parse("127.0.0.1");
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress,port);
tcpListener.Start();
Console.WriteLine("Server Started") ;
//Accepts a new connection
Socket socketForClient = tcpListener.AcceptSocket();
//StreamWriter and StreamReader Classes for reading and writing the data to and from.
//The server reads the meassage sent by the Client ,converts it to upper case and sends it back to the client.
//Lastly close all the streams.
try
{
if (socketForClient.Connected)
{
while(true)
{
Console.WriteLine("Client connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);
string line = streamReader.ReadLine();
Console.WriteLine("Read:" +line);
line=line.ToUpper()+ "!";
streamWriter.WriteLine(line);
Console.WriteLine("Wrote:"+line);
streamWriter.Flush() ;
}
}
socketForClient.Close();
Console.WriteLine("Exiting");
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
}
}
Client,客户端程序,在文本框中输入字符,将在列表框显示。
using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace SocketSample
{
public class Sample : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btS;
private System.Windows.Forms.TextBox t1;
private NetworkStream networkStream ;
private StreamReader streamReader ;
private StreamWriter streamWriter ;
ArrayList sb;
TcpClient myclient;
bool flag=false;
private System.Windows.Forms.ListBox t2;
private System.ComponentModel.Container components = null;
public Sample()
{
sb = new ArrayList();
InitializeComponent();
if(!flag)
Connect();
//get a Network stream from the server
networkStream = myclient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
ShowMessage();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**////
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Sample));
this.t1 = new System.Windows.Forms.TextBox();
this.btS = new System.Windows.Forms.Button();
this.t2 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// t1
//
this.t1.Location = new System.Drawing.Point(24, 32);
this.t1.Name = "t1";
this.t1.Size = new System.Drawing.Size(280, 21);
this.t1.TabIndex = 0;
this.t1.Text = "";
this.t1.TextChanged += new System.EventHandler(this.t1_TextChanged);
//
// btS
//
this.btS.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btS.BackgroundImage")));
this.btS.Enabled = false;
this.btS.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btS.Location = new System.Drawing.Point(320, 32);
this.btS.Name = "btS";
this.btS.TabIndex = 1;
this.btS.Text = "Send";
this.btS.Click += new System.EventHandler(this.btS_Click);
//
// t2
//
this.t2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.t2.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.t2.ItemHeight = 15;
this.t2.Location = new System.Drawing.Point(24, 64);
this.t2.Name = "t2";
this.t2.Size = new System.Drawing.Size(368, 212);
this.t2.TabIndex = 2;
//
// Sample
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(416, 297);
this.Controls.Add(this.t2);
this.Controls.Add(this.btS);
this.Controls.Add(this.t1);
this.Name = "Sample";
this.Text = "Sample";
this.ResumeLayout(false);
}
#endregion
public static void Main()
{
Sample df=new Sample();
df.FormBorderStyle=FormBorderStyle.Fixed3D;
Application.Run(df);
}
protected void Connect()
{
//connect to the "localhost" at the give port
//if you have some other server name then you can use that instead of "localhost"
try
{
sb.Add("Conneting to Server");
myclient = new TcpClient("localhost", 1234);
sb.Add("Conneted,Please enter something in the textbox");
}
catch
{
sb.Add(string.Format("Failed to connect to server at {0}:1234", "localhost"));
}
flag = true;
}
protected void ShowMessage()
{
for(int i=0;i<sb.Count;i++)
{
t2.Items.Add((object)sb.ToString());
}
sb.Clear();
}
private void t1_TextChanged(object sender, System.EventArgs e)
{
if(t1.Text == "" )
btS.Enabled = false;
else
btS.Enabled=true;
}
private void btS_Click(object sender, System.EventArgs e)
{
if(t1.Text=="")
{
sb.Add( "Please enter something in the textbox.");
t1.Focus();
return ;
}
string s;
try
{
streamWriter.WriteLine(t1.Text);
Console.WriteLine("Sending Message");
streamWriter.Flush();
s= streamReader.ReadLine();
Console.WriteLine("Reading Message") ;
Console.WriteLine(s) ;
sb.Add(s);
t1.Text = "";
t1.Focus();
ShowMessage();
}
catch
{
MessageBox.Show("Error.");
}
}
}
}
发表评论
-
Java.lang.ThreadLocal类
2012-01-20 09:02 416Java.lang.ThreadLocal类 2010 ... -
.net 线程概述
2012-01-20 09:02 500.net 线程概述 2011年06月23日 概论 多线 ... -
Borland 基础与应用开发课程认证试题整理集
2012-01-20 09:02 602Borland 基础与应用开发课程认证试题整理集 2010年 ... -
windows x64 vista以上系统代码完整性校验分析
2012-01-20 09:02 1644windows x64 vista以上系统代码完整性校验分析 ... -
C1X系列: 多线程(N1494)
2012-01-20 09:02 714C1X系列: 多线程(N1494) 2010年08月03日 ... -
VBS整人代码 很多 测试把我给整安逸了
2012-01-19 14:06 2253VBS整人代码 很多 测试把我给整安逸了 2010年08月2 ... -
利用VBS让QQ永远在线
2012-01-19 14:06 557利用VBS让QQ永远在线 201 ... -
VBS Runas 自动明文的输入密码
2012-01-19 14:06 698VBS Runas 自动明文的输入密码 2010年06月23 ... -
VBS运用之妙用SendKeys
2012-01-19 14:06 902VBS运用之妙用SendKeys 2010年11月12日 ... -
了解VBE!VBS教程!
2012-01-19 14:06 684了解VBE!VBS教程! 2011年09 ... -
使用Windbg排除蓝屏故障(已发表)
2012-01-17 03:56 634使用Windbg排除蓝屏故障(已发表) 2011年09月28 ... -
系统的一些解决方法
2012-01-17 03:56 604系统的一些解决方法 20 ... -
2011-11-1
2012-01-17 03:56 4662011-11-1 2011年11月01日 第一篇:一 ... -
Linux下FrameBuffer直接写屏
2012-01-17 03:56 982Linux下FrameBuffer直接写屏 2011年09月 ... -
exe文件不能运行的解决方法
2012-01-17 03:56 1032exe文件不能运行的解决 ... -
Socket基础
2012-01-16 02:45 542Socket基础 2010年04月02日 Socket基 ... -
一个类似ifconfig功能的程序代码--C语言(ZZ)
2012-01-16 02:44 952一个类似ifconfig功能的程序代码--C语言(ZZ) 2 ... -
tcp简单实现C/S模式程序,测试成功
2012-01-16 02:44 1094tcp简单实现C/S模式程序,测试成功 2010年03月25 ... -
用C#做远程监控程序
2012-01-16 02:44 3148用C#做远程监控程序 2009年12月05日 最近在做的 ...
相关推荐
C# SOCKET编程实例 ,以飞鸽传书为案例,相当不错
c# socket编程实例 c# socket编程实例
C#socket编程从入门到精通,学习网络编程以及socket的可以看下
c# socket编程 一个简单的模拟QQ聊天软件 可以群聊
c# socket 网络编程 经典 实例 源码
C# Socket编程
C#Socket编程实例,一个比较不错的参考实例
一个最基本的C# Socket编程小例子,适合初学者。
C#raw Socket编程C#raw Socket编程 实现raw Socket编程
C#socket网络编程.pdf C#socket网络编程.pdf
c#简单的socket 编程 客户端发送字符串 服务端接收返回信息
C# Socket编程完成控制台服务器端和web客户端(调试通过)
C# SOCKET编程样例
c# socket编程实例一 FTPserver源码,非常经典的源码。或者访问http://www.tangshansoft.cn
C# 最基本的Socket编程C# Socke详解
C#_SOCKET编写的简单聊天通信程序(客户端+服务端)
10个C#Socket编程代码示例.zip
socket编程实例 源代码 用c#编写的客户端、服务器模板