现在的位置: 首页 > 综合 > 正文

简单的Soket通信

2013年04月06日 ⁄ 综合 ⁄ 共 4069字 ⁄ 字号 评论关闭

今天碰见一个Case说要占掉一个端口再运行。那好,写个Socket让他一直通信,也就占用端口了:

TCP

 

代码

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

namespace ConsoleApplication1.SocketTest
{
class SocketTCP
{
private static void Server()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint SerIP
= new IPEndPoint(IPAddress.Any, 8888);
Socket serSock
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serSock.Bind(SerIP);
serSock.Listen(
5);

Console.WriteLine(
"Waiting the client.....");

Socket cliSock
= serSock.Accept();
IPEndPoint CliIP
= cliSock.RemoteEndPoint as IPEndPoint;
Console.WriteLine(
"connect with client:" + CliIP.Address + " at port:" + CliIP.Port);
string welcome = "welcome here!";

data
= Encoding.ASCII.GetBytes(welcome);
cliSock.Send(data, data.Length, SocketFlags.None);
//发送信息
while (true)
{
//用死循环来不断的从客户端获取信息
data = new byte[1024];
recv
= cliSock.Receive(data);
Console.WriteLine(
"recv=" + recv);
if (recv == 0)//当信息长度为0,说明客户端连接断开
break;
Console.WriteLine(Encoding.ASCII.GetString(data,
0, recv));
cliSock.Send(data, recv, SocketFlags.None);
}
Console.WriteLine(
"Disconnected from" + CliIP.Address);
cliSock.Close();
serSock.Close();
}

private static void client()
{
byte[] data = new byte[1024];
Socket Client
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint IP
= new IPEndPoint(IPAddress.Parse("172.18.6.249"), 8888);
try
{
Client.Connect(IP);
}
catch (Exception e)
{
Console.WriteLine(
"unable to connect to server");
Console.WriteLine(e.ToString());
return;
}

int recv = Client.Receive(data);
string strdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(strdata);
while (true)
{
string input = Console.ReadLine();
if (input == "exit")
break;
Client.Send(Encoding.ASCII.GetBytes(input));
data
= new byte[1024];
recv
= Client.Receive(data);
strdata
= Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(strdata);
}
Console.WriteLine(
"disconnect from sercer");
Client.Shutdown(SocketShutdown.Both);
Client.Close();

}
public static void Run()
{
Server();
new Thread(client).Start();
}
}
}

UDP

 

代码

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

namespace ConsoleApplication1.SocketTest
{
class SockUDP
{
private static void client()
{
byte[] data = new byte[1024];//定义一个数组用来做数据的缓冲区
string input, stringData;
IPEndPoint ipep
= new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello,are you there?";
data
= Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
//将数据发送到指定的终结点

IPEndPoint sender
= new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote
= (EndPoint)sender;
data
= new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);//接受来自服务器的数据

Console.WriteLine(
"Message received from{0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data,
0, recv));
while (true)//读取数据
{
input
= Console.ReadLine();//从键盘读取数据
if (input == "text")//结束标记
{
break;
}
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
//将数据发送到指定的终结点Remote
data = new byte[1024];
recv
= server.ReceiveFrom(data, ref Remote);//从Remote接受数据
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine(
"Stopping client");
server.Close();

}
private static void Server()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep
= new IPEndPoint(IPAddress.Any, 9050);//定义一网络端点
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//定义一个Socket
newsock.Bind(ipep);//Socket与本地的一个终结点相关联
Console.WriteLine("Waiting for a client..");

IPEndPoint sender
= new IPEndPoint(IPAddress.Any, 0);//定义要发送的计算机的地址
EndPoint Remote = (EndPoint)(sender);//
recv = newsock.ReceiveFrom(data, ref Remote);//接受数据
Console.WriteLine("Message received from{0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data,
0, recv));

string welcome = "Welcome to my test server!";
data
= Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data
= new byte[1024];
recv
= newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data,
0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}

public static void Run()
{
Server();
}
}
}

有时间的研究研究异步的了。。。

抱歉!评论已关闭.