这两天刚写了个,顺便贴出来大家分享一下 Server端: /** * This file establish a socket communication at the server part. * @author Yijun Zhu * @version 1.3 Date:2006-11-16 * @version 1.2 Date:2006-11-15 * @version 1.1 Date:2006-11-14 * @version 1.0 Date:2006-11-13 */ import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.Random;public class TcpServer extends Thread { public static Socket soc = null; // to send a proverb to the client public static String str = null; // set the socket communicate port public static int m_intPort = 8000; ArrayList arr = new ArrayList(); public static PrintWriter out = null; public TcpServer() { arr.add("An apple a day keeps doctors away."); arr.add("A friend in need is a friend indeed."); arr.add("Don not gild the lily."); arr.add("It is the tears of the earth that keep here smiles in bloom."); arr.add("Her wishful face haunts my dreams like the rain at night."); arr.add("I can live for two months on a good compliment."); arr.add("Happiness, I have discovered, is nearly always a rebound from hard work."); arr.add("It is impossible to enjoy idling thoroughly unless one has plenty of work to do. "); arr.add("Growth and change are the law of all life. "); arr.add("happy, happy, happy"); } public void run() { Random ran = new Random(); int ranInt = ran.nextInt(10); str = (String)arr.get(ranInt); try { // send words to the client by socket communicating out = new PrintWriter(soc.getOutputStream()); out.println(str); out.flush(); // System.out.println("Server start...."); } catch (IOException ex) { ex.printStackTrace(); } finally { if (null != out) { out.close(); } } } public static void main(String[] args) throws IOException { // establish a aptotic location to communicate ServerSocket svrsoc = null; try { svrsoc = new ServerSocket(m_intPort); while (true) { TcpServer ts = new TcpServer(); soc = svrsoc.accept(); ts.start(); } } catch (Exception ex) { ex.printStackTrace(); } finally { // close the socket at the Server part if (null != svrsoc) { svrsoc.close(); } // close the socket at the Client part if (null != soc) { try { soc.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } }
Client端: /** This file establish a socket communication at the client part. * @author Yijun Zhu * @version 1.3 Date:2006-11-16 * @version 1.2 Date:2006-11-15 * @version 1.1 Date:2006-11-14 * @version 1.0 Date:2006-11-13 */ import java.io.*; import java.net.*;public class TcpClient { // to get the words from the keyboard public static String str = null; // public static String strout = null; public static String m_strHost = "localhost"; // set the communication port public static int m_intPort = 8000; public static void main(String[] args) throws IOException { // establish a socket instance Socket soc = null; BufferedReader in = null; try { // Creates a stream socket and connects it // to the specified port number at the specified IP address soc = new Socket(m_strHost, m_intPort); // get the words from the socket communication in = new BufferedReader( new InputStreamReader(soc.getInputStream())); System.out.println("Connecting to the Server "); System.out.println(in.readLine()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (null != in) { in.close(); }
// close the socket at the server part if (null != soc) { soc.close(); } System.exit(0); } } }
Server端:
/**
* This file establish a socket communication at the server part.
* @author Yijun Zhu
* @version 1.3 Date:2006-11-16
* @version 1.2 Date:2006-11-15
* @version 1.1 Date:2006-11-14
* @version 1.0 Date:2006-11-13
*/
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Random;public class TcpServer extends Thread
{
public static Socket soc = null; // to send a proverb to the client
public static String str = null; // set the socket communicate port
public static int m_intPort = 8000; ArrayList arr = new ArrayList(); public static PrintWriter out = null; public TcpServer()
{
arr.add("An apple a day keeps doctors away.");
arr.add("A friend in need is a friend indeed.");
arr.add("Don not gild the lily.");
arr.add("It is the tears of the earth that keep here smiles in bloom.");
arr.add("Her wishful face haunts my dreams like the rain at night.");
arr.add("I can live for two months on a good compliment.");
arr.add("Happiness, I have discovered, is nearly always a rebound from hard work.");
arr.add("It is impossible to enjoy idling thoroughly unless one has plenty of work to do. ");
arr.add("Growth and change are the law of all life. ");
arr.add("happy, happy, happy");
} public void run()
{
Random ran = new Random();
int ranInt = ran.nextInt(10);
str = (String)arr.get(ranInt); try
{
// send words to the client by socket communicating
out = new PrintWriter(soc.getOutputStream());
out.println(str);
out.flush();
// System.out.println("Server start....");
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (null != out)
{
out.close();
}
}
} public static void main(String[] args) throws IOException
{
// establish a aptotic location to communicate
ServerSocket svrsoc = null; try
{
svrsoc = new ServerSocket(m_intPort); while (true)
{
TcpServer ts = new TcpServer();
soc = svrsoc.accept();
ts.start();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
// close the socket at the Server part
if (null != svrsoc)
{
svrsoc.close();
} // close the socket at the Client part
if (null != soc)
{
try
{
soc.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
}
/** This file establish a socket communication at the client part.
* @author Yijun Zhu
* @version 1.3 Date:2006-11-16
* @version 1.2 Date:2006-11-15
* @version 1.1 Date:2006-11-14
* @version 1.0 Date:2006-11-13
*/
import java.io.*; import java.net.*;public class TcpClient
{
// to get the words from the keyboard public static
String str = null; // public static String strout = null;
public static String m_strHost = "localhost"; // set the communication port
public static int m_intPort = 8000; public static void main(String[] args) throws IOException
{
// establish a socket instance
Socket soc = null; BufferedReader in = null; try
{
// Creates a stream socket and connects it // to the
specified port number at the specified IP address soc = new
Socket(m_strHost, m_intPort); // get the words from the socket communication in = new
BufferedReader( new InputStreamReader(soc.getInputStream()));
System.out.println("Connecting to the Server ");
System.out.println(in.readLine());
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (null != in)
{
in.close();
}
// close the socket at the server part
if (null != soc)
{
soc.close();
} System.exit(0);
}
}
}