c#下的Socket网络通信(类库)
来源:互联网 发布:中国网络焦点台 编辑:程序博客网 时间:2024/04/24 06:22
声明:单线程 (多线程 尚待完善)
环境: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");
}
}
}
}
- c#下的Socket网络通信(类库)
- 多线程下的Socket网络通信
- Windows下socket网络通信
- UDP: 用Socket 实现UDP 协议下的网络通信
- C# 利用socket实现语音网络通信
- C# Socket网络通信初探(一)
- JAVA的网络通信----SOCKET
- windows下socket 网络通信编程二
- android 3G网络下Socket通信
- windows下使用socket网络进程通信
- linux下socket网络通信详解
- C#下的Raw Socket编程实现网络封包监视
- C#下的Raw Socket编程实现网络封包监视
- C#下的Raw Socket编程实现网络封包监视
- flex下的socket通信
- linux下的socket通信
- vc的网络通信--Socket编程
- 移动设备的网络通信 http /socket
- 使用XPathExpression类对XML进行排序
- 一个JAVA读写文件的例子,主要是熟悉对字符流的应用(自己学习用)
- pku3117
- 戈尔迪斯结
- VB编程 WebBrowser 控件的属性与方法
- c#下的Socket网络通信(类库)
- pku3126
- 遭遇修改系统时间、使用映像劫持的xibgptd.exe,netdde32.exe等1
- 说实话,写了这么多程序了,还从来没有用JUnit作为单元测试工具测试过,今天就来学习一下
- 类似Dialog的Layer
- WebBrowser控件使用详解
- c#中的反射
- AXIS学习笔记
- pku3338