很久之前用过Socket,由于近期项目需要用socket传输视频流,早上花了半小时回顾了一下。
Socket类简要介绍
socket下提供的主要类有四个,分别是socket,tcpclient,tcplistener,udpclient,他们的作用如下:
- Socket 含有大量方法用于网络连接,.net中的一些高级类如webrequest,tcpclient等均调用此类来进行操作
- TcpClient 提供客户端连接,用于创建和使用tcp网络服务
- TcpListener 用于从tcp网络客户端侦听连接
- UdpClient 为udp客户端创建连接,是tcp的备用协议,在本地网络使用
Demo测试
这个Demo含有两个project,一个是server,一个是client,为了简洁,均采用控制台程序
服务端:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace SocketServer
{
class Program
{
public static TcpListener tcpListener;
private static NetworkStream networkStream;
private static StreamReader streamReader;
private static StreamWriter streamWriter;
public static Socket socketForClient;
static void Main(string[] args)
{
try
{
tcpListener = new TcpListener(1234);
tcpListener.Start();
Console.WriteLine("服务启动,开始监控1234端口");
socketForClient = tcpListener.AcceptSocket();
networkStream = new NetworkStream(socketForClient);
streamWriter = new StreamWriter(networkStream);
streamReader = new StreamReader(networkStream);
try
{
if (socketForClient.Connected)
{
Console.WriteLine("客户端已连接");
string strInput = streamReader.ReadLine();
Console.WriteLine("从客户端传入的字符串是:" + strInput);
string strOutput = strInput.ToUpper();
streamWriter.Write(strOutput);
Console.WriteLine("转换后字符串是" + strOutput);
streamWriter.Flush();
}
}
catch (Exception ex)
{
Console.WriteLine("出现异常:" + ex.Message);
}
}
catch(Exception ex)
{
Console.WriteLine("发生异常,关闭连接");
tcpListener.Stop();
streamWriter.Close();
streamReader.Close();
networkStream.Close();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace SocketServer
{
class Program
{
public static TcpListener tcpListener;
private static NetworkStream networkStream;
private static StreamReader streamReader;
private static StreamWriter streamWriter;
public static Socket socketForClient;
static void Main(string[] args)
{
try
{
tcpListener = new TcpListener(1234);
tcpListener.Start();
Console.WriteLine("服务启动,开始监控1234端口");
socketForClient = tcpListener.AcceptSocket();
networkStream = new NetworkStream(socketForClient);
streamWriter = new StreamWriter(networkStream);
streamReader = new StreamReader(networkStream);
try
{
if (socketForClient.Connected)
{
Console.WriteLine("客户端已连接");
string strInput = streamReader.ReadLine();
Console.WriteLine("从客户端传入的字符串是:" + strInput);
string strOutput = strInput.ToUpper();
streamWriter.Write(strOutput);
Console.WriteLine("转换后字符串是" + strOutput);
streamWriter.Flush();
}
}
catch (Exception ex)
{
Console.WriteLine("出现异常:" + ex.Message);
}
}
catch(Exception ex)
{
Console.WriteLine("发生异常,关闭连接");
tcpListener.Stop();
streamWriter.Close();
streamReader.Close();
networkStream.Close();
}
}
}
}
客户端:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace SocketClient
{
class Program
{
public static TcpClient myclient;
private static NetworkStream networkStream;
private static StreamReader streamReader;
private static StreamWriter streamWriter;
static void Main(string[] args)
{
try
{
try
{
myclient = new TcpClient("localhost", 1234);
Console.WriteLine("已经连接到localhost的1234端口");
}
catch (Exception ex)
{
Console.WriteLine("未能在1234端口连接上服务器");
}
networkStream = myclient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
Console.WriteLine("请输入一串小写字母:");
string strInput = Console.ReadLine();
streamWriter.Write(strInput);
Console.WriteLine("正在发送消息");
streamWriter.Flush();
string strServerComes = streamReader.ReadLine();
Console.WriteLine("正在从服务端收取消息");
Console.WriteLine("从服务端收到的消息是:" + strServerComes);
}
catch(Exception ex)
{
Console.WriteLine("发生异常,关闭连接");
streamReader.Close();
streamWriter.Close();
networkStream.Close();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace SocketClient
{
class Program
{
public static TcpClient myclient;
private static NetworkStream networkStream;
private static StreamReader streamReader;
private static StreamWriter streamWriter;
static void Main(string[] args)
{
try
{
try
{
myclient = new TcpClient("localhost", 1234);
Console.WriteLine("已经连接到localhost的1234端口");
}
catch (Exception ex)
{
Console.WriteLine("未能在1234端口连接上服务器");
}
networkStream = myclient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
Console.WriteLine("请输入一串小写字母:");
string strInput = Console.ReadLine();
streamWriter.Write(strInput);
Console.WriteLine("正在发送消息");
streamWriter.Flush();
string strServerComes = streamReader.ReadLine();
Console.WriteLine("正在从服务端收取消息");
Console.WriteLine("从服务端收到的消息是:" + strServerComes);
}
catch(Exception ex)
{
Console.WriteLine("发生异常,关闭连接");
streamReader.Close();
streamWriter.Close();
networkStream.Close();
}
}
}
}
在windows 7 中需要安全设置,否则socket将无法正常访问端口。
接下来将会做socket与silverlight传输文本及视频流的尝试。