c#下的Socket网络通信(类库) (转载)

上一篇 / 下一篇  2007-08-11 00:00:00 / 个人分类:一般分类

声明:单线程 (多线程 尚待完善)

环境:VS.NET2005;

界面 : dos;

语言 :C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Server
{
public class comunicate
{
Socket listenSocket;
IPAddress serverIP;
IPEndPoint endp;
public comunicate()
{
Console.WriteLine("begin Create Socket......");
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverIP = IPAddress.Parse("127.0.0.1");
endp = new IPEndPoint(serverIP, 2000);
}
public void SocketBind()
{
Console.WriteLine("begin Bind Socket......");
listenSocket.Bind(endp);
}
public void SocketListen()
{
Console.WriteLine("begin Listen......");
listenSocket.Listen(20);
}
public Socket AcceptMethod()
{
Console.WriteLine("wait Accept......");
Socket mSocket = listenSocket.Accept();
return mSocket;
}
public void SendReceiveTest(Socket server)//不能用static
{


Console.WriteLine("请输入:");

byte[] msg = Encoding.UTF8.GetBytes(Console.ReadLine());
byte[] bytes = new byte[256];
try
{ //阻塞直到发送返回
int byteCount = server.Send(msg, SocketFlags.None);
byteCount = server.Receive(bytes, SocketFlags.None);
string str1 = Encoding.UTF8.GetString(bytes,0,byteCount);
if (byteCount > 0)
{
Console.WriteLine("已接收:{0}",str1);
}

}
catch (SocketException e)
{
Console.WriteLine("{0} Error code:{1}.", e.Message, e.ErrorCode);

}
}

public void SocketClose()
{
this.listenSocket.Shutdown(SocketShutdown.Both);
this.listenSocket.Close();
}
}

}

namespace Client
{
public class comunicate
{
private IPAddress serverIP;
private IPEndPoint serverFullAddr;
private Socket sock;

public comunicate()
{
serverIP=IPAddress.Parse("127.0.0.1");
serverFullAddr = new IPEndPoint(serverIP, 2000);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

public void SocketConn()
{
Console.WriteLine(" begin connect");
sock.Connect(serverFullAddr);
}

public void ReceiveMsg()
{
try
{
Console.WriteLine("等待接收");
byte[] byteRec = new byte[256];
int len = this.sock.Receive(byteRec);
string strRec = System.Text.Encoding.UTF8.GetString(byteRec, 0, len);
Console.WriteLine("已接收:{0}", strRec);

}
catch(Exception ex)
{
Console.WriteLine("Receive Message Error"+ex.Message);
}
}
public void SocketSend()//public?
{
Console.WriteLine("回答:");
byte[] byteSend =
System.Text.Encoding.UTF8.GetBytes(Console.ReadLine());
try
{
this.sock.Send(byteSend);
}
catch
{
Console.WriteLine("Send Message Error");
}
}
public void SocketClose()
{
try
{
this.sock.Shutdown(SocketShutdown.Receive);
this.sock.Close();
}
catch
{
Console.WriteLine("Exit Error");
}
}
}
}



TAG:

引用 删除 dd   /   2010-12-22 11:16:40
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2012-02-09  
   1234
567891011
12131415161718
19202122232425
26272829   

数据统计

  • 访问量: 16977
  • 日志数: 676
  • 建立时间: 2007-12-21
  • 更新时间: 2008-02-29

RSS订阅

Open Toolbar