这里有一个服务端的程序,监听的端口是4554,在路由器上把外网的这个端口映射过来就行了。using System; using System.Net.Sockets; using System.Net ; using System.Threading ;public class DateServer { private TcpListener myListener ; private int port = 4554 ;
public DateServer() { try { //start listing on the given port myListener = new TcpListener(port) ; myListener.Start(); Console.WriteLine("Server Ready - Listining for new Connections ...") ; //start the thread which calls the methor SatrtListen Thread th = new Thread(new ThreadStart(StartListen)); th.Start() ; } catch(Exception e) { Console.WriteLine("An Exception Occured while Listing :"+e.ToString()); }
} public static void Main(String[] argv) { DateServer dts = new DateServer(); }
public void StartListen() { while(true) { //Accept a new connection Socket mySocket = myListener.AcceptSocket() ; if(mySocket.Connected) { Console.WriteLine("Client Connected!!") ; //make a byte array and receive data from the client Byte[] receive = new Byte[64] ; int i=mySocket.Receive(receive,receive.Length,0) ; char[] unwanted = {' ',' ',' '}; string rece = System.Text.Encoding.ASCII.GetString(receive); Console.WriteLine(rece.TrimEnd(unwanted)) ;
//get the current date/time and convert it to string DateTime now = DateTime.Now; String strDateLine ="Server: The Date/Time Now is: "+ now.ToShortDateString() + " " + now.ToShortTimeString();
// Convert to byte array and send Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray()); mySocket.Send(byteDateLine,byteDateLine.Length,0); } } } }
这是客户端代码using System ; using System.Net.Sockets ; using System.Net ; using System.Threading ;public class DateClient {
//the needed member feilds private TcpClient tcpc; private string name ; private int port=4554 ; private bool readData=false ;
public DateClient(string name) { //a label tryagain :
this.name=name ; try { //connect to the "localhost" at the give port //if you have some other servername then you can use that //instead of "localhost" tcpc =new TcpClient("localhost",port) ; //get a Network stream from the server NetworkStream nts = tcpc.GetStream() ; //if the stream is writiable then write to the server if(nts.CanWrite) { string sender = "Hi Server I am "+name ; Byte[] sends = System.Text.Encoding.ASCII.GetBytes(sender.ToCharArray()); nts.Write(sends,0,sends.Length) ; //flush to stream nts.Flush() ; }
//make a loop to wait untill some data is read from the stream while(!readData&&nts.CanRead) { //if data available then read from the stream if(nts.DataAvailable) { byte[] rcd = new byte[128]; int i=nts.Read( rcd,0,128);
} catch(Exception e) { Console.WriteLine("Could not Connect to server because "+e.ToString()); //Here an exception can be cause if the client is started before starting the server. //A good technique is used to handle such exceptions and give the client //a chance to re-try to connect to the server Console.Write("Do you want to try Again? [y/n]: ") ; char check = Console.ReadLine().ToCharArray()[0]; if(check=='y'|| check=='Y') goto tryagain ; }
}
public static void Main(string[] argv) { //check to see if the client has entered his name //if not ask him if he wants to enter his name. if(argv.Length<=0) { Console.WriteLine("Usage: DataClient <yourname>") ; Console.Write("Would You like to enter your name now [y/n] ?") ; char check = Console.ReadLine().ToCharArray()[0]; if(check=='y'|| check=='Y') { Console.Write("Please enter you name :") ; string newname=Console.ReadLine(); DateClient dc = new DateClient(newname) ; Console.WriteLine("Disconnected!!") ; Console.ReadLine() ; }
} else { DateClient dc = new DateClient(argv[0]) ; Console.WriteLine("Disconnected!!") ; Console.ReadLine() ; } } }
假设内外网都连通的情况下,怎样让内外网的程序都能通知我的服务执行某个方法,不用webservice。
using System.Net.Sockets;
using System.Net ;
using System.Threading ;public class DateServer
{
private TcpListener myListener ;
private int port = 4554 ;
public DateServer()
{
try
{
//start listing on the given port
myListener = new TcpListener(port) ;
myListener.Start();
Console.WriteLine("Server Ready - Listining for new Connections ...") ;
//start the thread which calls the methor SatrtListen
Thread th = new Thread(new ThreadStart(StartListen));
th.Start() ;
}
catch(Exception e)
{
Console.WriteLine("An Exception Occured while Listing :"+e.ToString());
}
} public static void Main(String[] argv)
{
DateServer dts = new DateServer();
}
public void StartListen()
{
while(true)
{
//Accept a new connection
Socket mySocket = myListener.AcceptSocket() ;
if(mySocket.Connected)
{
Console.WriteLine("Client Connected!!") ;
//make a byte array and receive data from the client
Byte[] receive = new Byte[64] ;
int i=mySocket.Receive(receive,receive.Length,0) ;
char[] unwanted = {' ',' ',' '};
string rece = System.Text.Encoding.ASCII.GetString(receive);
Console.WriteLine(rece.TrimEnd(unwanted)) ;
//get the current date/time and convert it to string
DateTime now = DateTime.Now;
String strDateLine ="Server: The Date/Time Now is: "+ now.ToShortDateString() + " " + now.ToShortTimeString();
// Convert to byte array and send
Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray());
mySocket.Send(byteDateLine,byteDateLine.Length,0);
}
}
}
}
using System.Net.Sockets ;
using System.Net ;
using System.Threading ;public class DateClient
{
//the needed member feilds
private TcpClient tcpc;
private string name ;
private int port=4554 ;
private bool readData=false ;
public DateClient(string name)
{
//a label
tryagain :
this.name=name ;
try
{
//connect to the "localhost" at the give port
//if you have some other servername then you can use that
//instead of "localhost"
tcpc =new TcpClient("localhost",port) ;
//get a Network stream from the server
NetworkStream nts = tcpc.GetStream() ;
//if the stream is writiable then write to the server
if(nts.CanWrite)
{
string sender = "Hi Server I am "+name ;
Byte[] sends = System.Text.Encoding.ASCII.GetBytes(sender.ToCharArray());
nts.Write(sends,0,sends.Length) ;
//flush to stream
nts.Flush() ;
}
//make a loop to wait untill some data is read from the stream
while(!readData&&nts.CanRead)
{
//if data available then read from the stream
if(nts.DataAvailable)
{
byte[] rcd = new byte[128];
int i=nts.Read( rcd,0,128);
string ree = System.Text.Encoding.ASCII.GetString(rcd);
char[] unwanted = {' ',' ',' '};
Console.WriteLine(ree.TrimEnd(unwanted)) ;
readData=true ;
}
}
}
catch(Exception e)
{
Console.WriteLine("Could not Connect to server because "+e.ToString());
//Here an exception can be cause if the client is started before starting the server.
//A good technique is used to handle such exceptions and give the client
//a chance to re-try to connect to the server
Console.Write("Do you want to try Again? [y/n]: ") ;
char check = Console.ReadLine().ToCharArray()[0];
if(check=='y'|| check=='Y')
goto tryagain ;
}
}
public static void Main(string[] argv)
{
//check to see if the client has entered his name
//if not ask him if he wants to enter his name.
if(argv.Length<=0)
{
Console.WriteLine("Usage: DataClient <yourname>") ;
Console.Write("Would You like to enter your name now [y/n] ?") ;
char check = Console.ReadLine().ToCharArray()[0];
if(check=='y'|| check=='Y')
{
Console.Write("Please enter you name :") ;
string newname=Console.ReadLine();
DateClient dc = new DateClient(newname) ;
Console.WriteLine("Disconnected!!") ;
Console.ReadLine() ; }
}
else
{
DateClient dc = new DateClient(argv[0]) ;
Console.WriteLine("Disconnected!!") ;
Console.ReadLine() ;
}
}
}