C-Sharp网络编程案例解析(Socket类的使用)

作者在 2013-11-15 22:14:30 发布以下内容

Server端:

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Net.Sockets;

namespace Server

{    

class Program    

{        

static void Main(string[] args)        

{            

//用于表示客户端发送的信息的长度            

int recvLenth;            

//用于缓存客户端所发送的信息,通过socket传递的信息必须为字节数组            

byte[] data=new byte[1024];            

//本机预使用的IP和端口            

IPEndPoint iend = new IPEndPoint(IPAddress.Any,9999);            

Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            

newsock.Bind(iend);            

newsock.Listen(10);            

Console.WriteLine("waiting for a client !");            

Socket client = newsock.Accept();            

//当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信            

IPEndPoint clientAdd = (IPEndPoint)client.RemoteEndPoint;//获取远程终结点            

Console.WriteLine("连接:"+clientAdd.Address+"端口:"+clientAdd.Port);//获取或设置终结点端口号            

string welcome = "welcome here !";            

data = Encoding.ASCII.GetBytes(welcome);            

client.Send(data,data.Length,SocketFlags.None);//指定套接字的发送和接收行为            

while(true)            

{                

data=new byte[1024];                

recvLenth = client.Receive(data);                

if(recvLenth==0)//信息长度为0,说明客户端连接断开                

{                    

break;                

}                

Console.WriteLine(Encoding.ASCII.GetString(data,0,recvLenth));                

client.Send(data,recvLenth,SocketFlags.None);            

}            

Console.WriteLine("断开连接"+clientAdd.Address);            

client.Close();            

newsock.Close();        

}    

}

}

Client端:

using System;

using System.Collections.Generic;

using System.Text;

using System.Net.Sockets;

using System.Net;

namespace Client

{    

class Program    

{        

static void Main(string[] args)        

{            

byte[] data=new byte[1024];            

Socket newclient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            

Console.WriteLine("输入服务器地址:");            

string ipadd = Console.ReadLine();            

Console.WriteLine();            

Console.Write("输入服务器端口:");            

int port = Convert.ToInt32(Console.ReadLine());            

IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd),port);//服务器的ip和端口            

try            

{                

//因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的ip和端口,也不需要监听                

newclient.Connect(ie);            

}            

catch(SocketException ex)            

{                

Console.WriteLine("不能连接");                

Console.WriteLine(ex.ToString());                

return;            

}            

int recv = newclient.Receive(data);            

string stringdata = Encoding.ASCII.GetString(data,0,recv);            

Console.WriteLine(stringdata);            

while(true)            

{                

string input = Console.ReadLine();                

if(input=="exit")                

{                    

break;                

}                

newclient.Send(Encoding.Default.GetBytes(input));                

data=new byte[1024];                

recv = newclient.Receive(data);                

stringdata = Encoding.Default.GetString(data,0,recv);                

Console.WriteLine(stringdata);            

}            

Console.WriteLine("断开连接");            

newclient.Shutdown(SocketShutdown.Both);            

newclient.Close();        

}    

}

}

默认分类 | 阅读 1824 次
文章评论,共0条
游客请输入验证码
浏览18304次
文章分类
最新评论