下面的例子是msdn里的,你看看const int portNumber = 13; TcpListener tcpListener = new TcpListener(portNumber); tcpListener.Start();Console.WriteLine("Waiting for a connection....");
try{ //Accept the pending client connection and return a TcpClient initialized for communication. TcpClient tcpClient = tcpListener.AcceptTcpClient(); Console.WriteLine("Connection accepted."); NetworkStream networkStream = tcpClient.GetStream(); string responseString = "You have successfully connected to me"; Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString); networkStream.Write(sendBytes, 0, sendBytes.Length); Console.WriteLine("Message Sent /> : " + responseString);
//Any communication with the remote client using the TcpClient can go here. // //////// //Close TcpListener and TcpClient. tcpClient.Close(); tcpListener.Stop(); } catch (Exception e) { Console.WriteLine(e.ToString()); }
IPAddress hostadd = Dns.Resolve("www.google.com").AddressList[0]; IPEndPoint EPhost = new IPEndPoint(hostadd,portNum); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp ); s.Connect(EPhost); Console.WriteLine(s.Connected); System.Net.Sockets.NetworkStream ns=new NetworkStream(s,System.IO.FileAccess.ReadWrite); byte[] str=new Byte[10240]; Encoding asc=Encoding.ASCII; byte[] snd; snd=asc.GetBytes("GET / HTTP/1.1\r\n\r\n");//获取根目录的文件 ns.Write(snd,0,snd.Length); int rcvCount=ns.Read(str,0,str.Length); Console.WriteLine(asc.GetString(str,0,rcvCount)); rcvCount=ns.Read(str,0,str.Length); Console.WriteLine(asc.GetString(str,0,rcvCount)); s.Shutdown(SocketShutdown.Both); Console.ReadLine(); s.Close();
using System; using System.Net.Sockets; using System.Text; using System.Net; public class TcpTimeServer{ private const int portNum = 80; public static void Main(String[] args){ IPAddress clientadd=IPAddress.Parse("211.150.254.56"); IPEndPoint EPclient=new IPEndPoint(clientadd,portNum); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp ); try{ s.Bind(EPclient); } catch (Exception e){ Console.WriteLine("Winsock error: " + e.ToString()); } s.Shutdown(SocketShutdown.Both); s.Close(); } }s.Bind(EPclient);出错,不让我bind 80端口? 是已经被ie占用了吗?211.150.254.56是我的ip
tcpListener.Start ( ) ;
TcpListener tcpListener = new TcpListener(portNumber); tcpListener.Start();Console.WriteLine("Waiting for a connection....");
try{ //Accept the pending client connection and return a TcpClient initialized for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connection accepted."); NetworkStream networkStream = tcpClient.GetStream(); string responseString = "You have successfully connected to me"; Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length); Console.WriteLine("Message Sent /> : " + responseString);
//Any communication with the remote client using the TcpClient can go here.
//
//////// //Close TcpListener and TcpClient.
tcpClient.Close();
tcpListener.Stop(); }
catch (Exception e) {
Console.WriteLine(e.ToString());
}
IPEndPoint EPhost = new IPEndPoint(hostadd,portNum);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp );
s.Connect(EPhost);
Console.WriteLine(s.Connected);
System.Net.Sockets.NetworkStream ns=new NetworkStream(s,System.IO.FileAccess.ReadWrite);
byte[] str=new Byte[10240];
Encoding asc=Encoding.ASCII;
byte[] snd;
snd=asc.GetBytes("GET / HTTP/1.1\r\n\r\n");//获取根目录的文件
ns.Write(snd,0,snd.Length);
int rcvCount=ns.Read(str,0,str.Length);
Console.WriteLine(asc.GetString(str,0,rcvCount));
rcvCount=ns.Read(str,0,str.Length);
Console.WriteLine(asc.GetString(str,0,rcvCount));
s.Shutdown(SocketShutdown.Both);
Console.ReadLine();
s.Close();
using System.Net.Sockets;
using System.Text;
using System.Net;
public class TcpTimeServer{
private const int portNum = 80;
public static void Main(String[] args){
IPAddress clientadd=IPAddress.Parse("211.150.254.56");
IPEndPoint EPclient=new IPEndPoint(clientadd,portNum);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp );
try{
s.Bind(EPclient);
}
catch (Exception e){
Console.WriteLine("Winsock error: " + e.ToString());
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
}s.Bind(EPclient);出错,不让我bind 80端口?
是已经被ie占用了吗?211.150.254.56是我的ip