关于.NET SOCKET编程
现在在做关于.netsocket编程,网上找了个实例,里面包括服务器端,客户端,现在我都弄好了,我该怎么来运行,两个一起运行么?不太明白该怎么来看效果,单个运行的话看不出...
现在在做关于.net socket编程,网上找了个实例,里面包括服务器端,客户端,现在我都弄好了,我该怎么来运行,两个一起运行么?不太明白该怎么来看效果,单个运行的话看不出有什么结果
问一下,端口有个复用,系统占用的端口好像监听不了 展开
问一下,端口有个复用,系统占用的端口好像监听不了 展开
4个回答
展开全部
两个都得运行呀。一个等待,一个接收
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先启动服务器端,服务器端会监听有没有客户端的请求,然后你启动客户端,执行你设计的操作,看服务器端能否收到,收到说明连接成功,执行服务器端的操作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
6.UDP对时服务器端
/*
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
*/
public void start_server()
{
while(true)
{
byte[] recData=server.Receive(ref receivePoint);
ASCIIEncoding encode=new ASCIIEncoding();
string Read_str=encode.GetString(recData));
string[] tem=Read_str.Split('/');
byte[] sendData=encode.GetBytes(DataTime.Now.ToString());
server.Send(sendData,sendData.Length,temp[0],Int32.Parse(temp[1]));
}
}
UdpClient server=new UdpClient(%%1);
IPEndPoint receivePoint=new IPEndPoint(new IPAddress("127001",%%1);
Thread startServer=new Thread(new ThreadStart(start_server));
startServer.Start();
protected override void Dispose(bool disposing)
{
try{
startServer.Abort();
server.Close();
}
catch{}
if(disposing && component!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
7.UDP对时客户端
/*
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
*/
[DllImport("Kernel32.dll")]
private static bool SetSystemTime(SystemTime time);
public class SystemTime
{
public short year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}
UdpClient client=new UdpClient(%%2);
IPEndPoint receivePoint=new IPEndPoint(a,%%2);
IPAddress a=new IPAddress.Parse("127001");
string timeString=DateTime.Now.ToString();
DataTime temp;
IPAddress HostIP;
bool continueLoop=true;
while(continueLoop)
{
string hostName=Dns.GetHostName();
Text.ASCIIEncoding encode=new Text.ASCIIEncoding();
string sendString=hostName+"/"+%%2.ToString();
byte[] sendData=encode.GetBytes(sendString);
byte[] recData;
try{
HostIP=IPAddress.Parse(%%1);
}
catch {
client.Send(sendData,sendData.Length,%%1,%%2);
recData=client.Receive(ref receivePoint);
timeString=encode.GetString(recData);
client.Close();
continueLoop=false;
return;
}
IPEndPoint host=new IPEndPoint(HostIP,%%2);
client.Send(sendData,sendData.Length,host);
recData=client.Receive(ref receivePoint);
timeString=encode.GetString(recData);
client.Close();
continueLoop=false;
}
try{
temp=DataTime.Parse(timeString);
}
catch {
return;
}
SystemTime st=new SystemTime();
st.year=(short)temp.Year;
st.Month=(short)temp.Month;
st.DayOfWeek=(short)temp.DayOfWeek;
st.Day=(short)temp.Day;
st.Hour=Conver.ToInt16(temp.Hour);
if(st.Hour>=12)
{
st.Hour-=(short)8;
}
else if(st.Hour>=8)
{
st.Hour-=(short)8;
}
else
{
st.Hour+=(short)16;
}
st.Minute=Convert.ToInt16(temp.Minute);
st.Second=Convert.ToInt16(temp.Second);
st.Milliseconds=Convert.ToInt16(temp.Milliseconds);
if(SetSystemTime(st))
{
//修改成功
}
else
{
//修改不成功
}
1.简单服务器端
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
*/
private static int port = %%2;
private static Thread thThreadRead;
private static TcpListener TcpListen;
private static bool bListener = true;
private static Socket stRead;
private static void Listen()
{
try
{
TcpListen = new TcpListener(port);
TcpListen.Start();
stRead = TcpListen.AcceptSocket();
EndPoint tempRemoteEP = stRead.RemoteEndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
string sHostName = host.HostName;
while (bListener)
{
stRead.Send(Encoding.ASCII.GetBytes("Hello"));
string sTime = DateTime.Now.ToShortTimeString();
Byte[] byRead = new Byte[1024];
int iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP);
Byte[] byText = new Byte[iRead];
Array.Copy(byRead, 0, byText, 0, iRead);
string line = System.Text.Encoding.Default.GetString(byRead);
}
}
catch (System.Security.SecurityException)
{
//监听失败
}
}
thThreadRead = new Thread(new ThreadStart(Listen));
thThreadRead.Start();
2.简单客户端
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
*/
private static IPEndPoint dateTimeHost;
string hostIPString=%%1;
string hostPortString=%%2;
IPAddress hostIP=IPAddress.Parse(hostIPString);
dateTimeHost=new IPEndPoint(hostIP,Int32.Parse(hostPortString));
Socket conn=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
conn.Connect(dateTimeHost);
int bytes=0;
Byte[] RecvBytes=new Byte[256];
bytes=conn.Receive(RecvBytes,RecvBytes.Length,0);
string RecvString=Encoding.ASCII.GetString(RecvBytes,0,bytes);
Console.WriteLine(RecvString);
conn.Shutdown(SocketShutdown.Both);
conn.Close();
3.获得本机IP
//using System.Net;
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
string %%1=null;
for (int i = 0; i < addressList.Length; i++)
{
%%1 += addressList[i].ToString();
}
4.端对端通信
/*
using System.Net;
using System.Net.Sockets;
*/
UdpClient client=new UdpClient(%%2);
IPAddress a=IPAddress.Parse("127001");
IPEndPoint receivePoint=new IPEndPoint(a,%%2);
IPAddress HostIP=null;
byte[] sendData=Encoding.UTF8.GetBytes(%%3);
byte[] recData;
try{
HostIP=IPAddress.Parse(%%1);
}
catch {
recData=client.Receive(ref receivePoint);
%%3=Encoding.UTF8.GetString(recData);
client.Send(sendData,sendData.Length,%%4,%%2);
client.Close();
}
IPEndPoint host=new IPEndPoint(HostIP,%%2);
recData=client.Receive(ref receivePoint);
%%3=Encoding.UTF8.GetString(recData);
client.Close();
5.点对点通信
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
*/
Thread th;
TcpListener tpListen1;
bool listenerRun=true;
NetworkStream tcpStream;
StreamWriter reqStreamW;
TcpClient tcpc;
Socket skSocket;
protected void Listen()
{
try{
tpListen1=new TcpListener(Int32.Parse(%%2));
tpListen1.Start();
skSocket=tpListen1.AcceptSocket();
EndPoint tempRemoteEP=skSocket.RemoteEndPoint;
IPEndPoint tempRemoteIP=(IPEndPoint)tempRemoteEP;
IPHostEntry host=Dns.GetHostByAddress(tempRemoteIP.Address);
string HostName=host.HostName;
while(listenerRun)
{
Byte[] stream=new Byte[1024];
string time=DateTime.Now.ToString();
int i=skSocket.ReceiveFrom(stream,ref tempRemoteEP);
string %%5=Encoding.UTF8.GetString(stream);
//指定编码,从缓冲区中解析出内容
//time+" "+HostName+":"
}
}
catch(Security.SecurityException)
{
//防火墙安全错误!
}
try{
string sMsg=%%4;
string MyName=Dns.GetHostName();
reqStreamW=new StreamWriter(tcpStream);
reqStreamW.Write(sMsg);
reqStreamW.Flush();
string time=DateTime.Now.ToString();
//显示传送的数据和时间
//time+" "+MyName+":"
//sMsg
}
catch(Exception)
{
//无法发送信息到目标计算机!
}
protected override void Dispose(bool disposing)
{
try{
listenerRun=false;
th.Abort();
th=null;
tpListen1.Stop();
skSocket.Close();
tcpc.Close();
}
catch{}
if(disposing && component!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
/*
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
*/
public void start_server()
{
while(true)
{
byte[] recData=server.Receive(ref receivePoint);
ASCIIEncoding encode=new ASCIIEncoding();
string Read_str=encode.GetString(recData));
string[] tem=Read_str.Split('/');
byte[] sendData=encode.GetBytes(DataTime.Now.ToString());
server.Send(sendData,sendData.Length,temp[0],Int32.Parse(temp[1]));
}
}
UdpClient server=new UdpClient(%%1);
IPEndPoint receivePoint=new IPEndPoint(new IPAddress("127001",%%1);
Thread startServer=new Thread(new ThreadStart(start_server));
startServer.Start();
protected override void Dispose(bool disposing)
{
try{
startServer.Abort();
server.Close();
}
catch{}
if(disposing && component!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
7.UDP对时客户端
/*
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
*/
[DllImport("Kernel32.dll")]
private static bool SetSystemTime(SystemTime time);
public class SystemTime
{
public short year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}
UdpClient client=new UdpClient(%%2);
IPEndPoint receivePoint=new IPEndPoint(a,%%2);
IPAddress a=new IPAddress.Parse("127001");
string timeString=DateTime.Now.ToString();
DataTime temp;
IPAddress HostIP;
bool continueLoop=true;
while(continueLoop)
{
string hostName=Dns.GetHostName();
Text.ASCIIEncoding encode=new Text.ASCIIEncoding();
string sendString=hostName+"/"+%%2.ToString();
byte[] sendData=encode.GetBytes(sendString);
byte[] recData;
try{
HostIP=IPAddress.Parse(%%1);
}
catch {
client.Send(sendData,sendData.Length,%%1,%%2);
recData=client.Receive(ref receivePoint);
timeString=encode.GetString(recData);
client.Close();
continueLoop=false;
return;
}
IPEndPoint host=new IPEndPoint(HostIP,%%2);
client.Send(sendData,sendData.Length,host);
recData=client.Receive(ref receivePoint);
timeString=encode.GetString(recData);
client.Close();
continueLoop=false;
}
try{
temp=DataTime.Parse(timeString);
}
catch {
return;
}
SystemTime st=new SystemTime();
st.year=(short)temp.Year;
st.Month=(short)temp.Month;
st.DayOfWeek=(short)temp.DayOfWeek;
st.Day=(short)temp.Day;
st.Hour=Conver.ToInt16(temp.Hour);
if(st.Hour>=12)
{
st.Hour-=(short)8;
}
else if(st.Hour>=8)
{
st.Hour-=(short)8;
}
else
{
st.Hour+=(short)16;
}
st.Minute=Convert.ToInt16(temp.Minute);
st.Second=Convert.ToInt16(temp.Second);
st.Milliseconds=Convert.ToInt16(temp.Milliseconds);
if(SetSystemTime(st))
{
//修改成功
}
else
{
//修改不成功
}
1.简单服务器端
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
*/
private static int port = %%2;
private static Thread thThreadRead;
private static TcpListener TcpListen;
private static bool bListener = true;
private static Socket stRead;
private static void Listen()
{
try
{
TcpListen = new TcpListener(port);
TcpListen.Start();
stRead = TcpListen.AcceptSocket();
EndPoint tempRemoteEP = stRead.RemoteEndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
string sHostName = host.HostName;
while (bListener)
{
stRead.Send(Encoding.ASCII.GetBytes("Hello"));
string sTime = DateTime.Now.ToShortTimeString();
Byte[] byRead = new Byte[1024];
int iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP);
Byte[] byText = new Byte[iRead];
Array.Copy(byRead, 0, byText, 0, iRead);
string line = System.Text.Encoding.Default.GetString(byRead);
}
}
catch (System.Security.SecurityException)
{
//监听失败
}
}
thThreadRead = new Thread(new ThreadStart(Listen));
thThreadRead.Start();
2.简单客户端
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
*/
private static IPEndPoint dateTimeHost;
string hostIPString=%%1;
string hostPortString=%%2;
IPAddress hostIP=IPAddress.Parse(hostIPString);
dateTimeHost=new IPEndPoint(hostIP,Int32.Parse(hostPortString));
Socket conn=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
conn.Connect(dateTimeHost);
int bytes=0;
Byte[] RecvBytes=new Byte[256];
bytes=conn.Receive(RecvBytes,RecvBytes.Length,0);
string RecvString=Encoding.ASCII.GetString(RecvBytes,0,bytes);
Console.WriteLine(RecvString);
conn.Shutdown(SocketShutdown.Both);
conn.Close();
3.获得本机IP
//using System.Net;
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
string %%1=null;
for (int i = 0; i < addressList.Length; i++)
{
%%1 += addressList[i].ToString();
}
4.端对端通信
/*
using System.Net;
using System.Net.Sockets;
*/
UdpClient client=new UdpClient(%%2);
IPAddress a=IPAddress.Parse("127001");
IPEndPoint receivePoint=new IPEndPoint(a,%%2);
IPAddress HostIP=null;
byte[] sendData=Encoding.UTF8.GetBytes(%%3);
byte[] recData;
try{
HostIP=IPAddress.Parse(%%1);
}
catch {
recData=client.Receive(ref receivePoint);
%%3=Encoding.UTF8.GetString(recData);
client.Send(sendData,sendData.Length,%%4,%%2);
client.Close();
}
IPEndPoint host=new IPEndPoint(HostIP,%%2);
recData=client.Receive(ref receivePoint);
%%3=Encoding.UTF8.GetString(recData);
client.Close();
5.点对点通信
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
*/
Thread th;
TcpListener tpListen1;
bool listenerRun=true;
NetworkStream tcpStream;
StreamWriter reqStreamW;
TcpClient tcpc;
Socket skSocket;
protected void Listen()
{
try{
tpListen1=new TcpListener(Int32.Parse(%%2));
tpListen1.Start();
skSocket=tpListen1.AcceptSocket();
EndPoint tempRemoteEP=skSocket.RemoteEndPoint;
IPEndPoint tempRemoteIP=(IPEndPoint)tempRemoteEP;
IPHostEntry host=Dns.GetHostByAddress(tempRemoteIP.Address);
string HostName=host.HostName;
while(listenerRun)
{
Byte[] stream=new Byte[1024];
string time=DateTime.Now.ToString();
int i=skSocket.ReceiveFrom(stream,ref tempRemoteEP);
string %%5=Encoding.UTF8.GetString(stream);
//指定编码,从缓冲区中解析出内容
//time+" "+HostName+":"
}
}
catch(Security.SecurityException)
{
//防火墙安全错误!
}
try{
string sMsg=%%4;
string MyName=Dns.GetHostName();
reqStreamW=new StreamWriter(tcpStream);
reqStreamW.Write(sMsg);
reqStreamW.Flush();
string time=DateTime.Now.ToString();
//显示传送的数据和时间
//time+" "+MyName+":"
//sMsg
}
catch(Exception)
{
//无法发送信息到目标计算机!
}
protected override void Dispose(bool disposing)
{
try{
listenerRun=false;
th.Abort();
th=null;
tpListen1.Stop();
skSocket.Close();
tcpc.Close();
}
catch{}
if(disposing && component!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询