Java.NET小例子

--Client.java

public class Client {

private static String ip = null;

private static String websiteName = null;

private static int port = 10000;

private static int time = 50000;

public static void main(String[] args) throws Exception{

/**

* 解析XML配置文件

*/

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(new File("/Work Files/Java/SocketNetworkWithXML/src/socketNetworkWithXML/Configure.xml"));

XML xml = new XML(document);

Element root = xml.getRoot();

NodeList nodeList = xml.getChild(root);

xml.getItem(nodeList);

ip = XML.ip;

websiteName = XML.websiteName;

port = XML.port;

time = XML.time;

/**

* 判断远程地址提供类型(ip/site name)

*/

if(ip == null && websiteName == null){

System.err.println("No Server Completed!");

System.exit(0);

}

else if(ip == null && websiteName != null){

try{

InetAddress aInetAddress = InetAddress.getByName(websiteName);

InetSocketAddress address = new InetSocketAddress(aInetAddress, port);

Socket socket = new Socket();

socket.connect(address, time);

ClientHandler clientHandler = new ClientHandler();

OutputStream outputStream = socket.getOutputStream();

PrintWriter printWriter = new PrintWriter(outputStream,true);

Scanner scanner = new Scanner(System.in);

InputStream inputStream = socket.getInputStream();

Scanner scanner2 = new Scanner(inputStream);

String string = null;

while(true){

clientHandler.send(scanner,printWriter);

string = clientHandler.receive(scanner2);

if(string.equals("Bye!")) break;

}

socket.close();

}

catch(Exception exception){

System.err.println("Server Not Found!");

}

}

else{

try{

InetSocketAddress address = new InetSocketAddress(ip, port);

Socket socket = new Socket();

socket.connect(address, time);

ClientHandler clientHandler = new ClientHandler();

OutputStream outputStream = socket.getOutputStream();

PrintWriter printWriter = new PrintWriter(outputStream,true);

Scanner scanner = new Scanner(System.in);

InputStream inputStream = socket.getInputStream();

Scanner scanner2 = new Scanner(inputStream);

String string = null;

while(true){

clientHandler.send(scanner,printWriter);

string = clientHandler.receive(scanner2);

if(string.equals("Bye!")) break;

}

socket.close();

}

catch (Exception exception) {

System.err.println("Server Not Found!");

}

}

}

}

--ClientHandler.java

public class ClientHandler {

private String string = null;

public void send(Scanner scanner, PrintWriter printWriter) throws Exception{

string = scanner.nextLine();

printWriter.println(string);

}

public String receive(Scanner scanner2) throws Exception{

string = scanner2.nextLine();

System.out.println(string);

return string;

}

}

--Server.java

public class Server {

private static int port = 10000;

public static void main(String[] args) throws Exception{

try{

ServerSocket serverSocket = new ServerSocket(port);

ServerHandler serverHandler = new ServerHandler(serverSocket);

try{

while(true) serverHandler.transform();

}

catch(Exception e){

System.err.println("Can Not Create Socket From Port:"+port+".!");

try{

serverHandler.close();

}

catch(Exception exception){

System.err.println("ServerSocket has already closed!");

}

finally{

System.exit(0);

}

}

}

catch(Exception exception){

System.err.println("Port was used by other applications!");

System.exit(0);

}

}

}

--ServerHandler.java

public class ServerHandler extends Socket implements Runnable{

private ServerSocket serverSocket;

private Socket socket;

public ServerHandler(ServerSocket serverSocket){

this.serverSocket = serverSocket;

}

public ServerHandler(Socket socket){

this.socket = socket;

}

public void transform() throws Exception{

Socket socket = serverSocket.accept();

Runnable runnable = new ServerHandler(socket);

Thread thread = new Thread(runnable);

thread.start();

}

public Scanner receive() throws Exception{

InputStream inputStream = socket.getInputStream();

Scanner scanner = new Scanner(inputStream);

return scanner;

}

public PrintWriter send() throws Exception{

OutputStream outputStream = socket.getOutputStream();

PrintWriter printWriter = new PrintWriter(outputStream,true);

return printWriter;

}

public void run(){

try{

Scanner scanner = receive();

PrintWriter printWriter = send();

String string = null;

while(scanner.hasNextLine()){

string = scanner.nextLine();

System.out.println(string);

if(string.equalsIgnoreCase("quit") || string.equalsIgnoreCase("exit")){

printWriter.println("Bye!");

break;

}

else{

printWriter.println("return: "+string);

}

}

scanner.close();

printWriter.close();

socket.close();

}

catch (Exception e){

e.printStackTrace();

}

}

}

--XML.java

public class XML {

private Document document;

public static String ip;

public static String websiteName;

public static int port;

public static int time;

public XML(Document document){

this.document = document;

}

public Element getRoot(){

Element root = document.getDocumentElement();

return root;

}

public NodeList getChild(Element root){

NodeList nodeList = root.getChildNodes();

return nodeList;

}

public void getItem(NodeList nodeList){

for(int i=0;i<nodeList.getLength();i++){

Node node = nodeList.item(i);

if(node instanceof Element){

Element element = (Element) node;

Text text = (Text) element.getFirstChild();

String string = text.getData().trim();

if(element.getTagName().equals("ip")) ip = string;

else if(element.getTagName().equals("websiteName")) websiteName = string;

else if(element.getTagName().equals("port")) port = Integer.parseInt(string);

else if(element.getTagName().equals("time")) time = Integer.parseInt(string);

else System.err.println("Unknown tag.");

}

}

}

}

--Configure.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<ip>127.0.0.1</ip>

<websiteName>null</websiteName>

<port>10000</port>

<time>50000</time>

</configuration>

原文地址:https://www.cnblogs.com/fusiji/p/11409867.html

时间: 2024-10-21 22:34:35

Java.NET小例子的相关文章

java反射小例子

package com.txwsqk.reflect; public class Car { private String brand; private String color; private int maxspeed; public Car(){} public Car(String brand, String color, int maxspeed){ this.brand = brand; this.color = color; this.maxspeed = maxspeed; }

java Dom小例子

仅仅是为了总结与学习. XML文件在我们的开发项目中经常使用,下面写了DOM解析的小例子. DomTestFile.xml文件如下: <?xml version="1.0" encoding="utf-8"?> <MenuContainer> <menu order="1.0" menuname="测试1" description="描述1"> <menu orde

Java调试小例子(如何使用F5和F6)

如果一个类有3个方法,就在3个方法的前面设置断点,总共就3个断点,然后进入Debug调试, 一直按F6(Step Over)他会在方法那里直接进入方法而不需要你按F5(Step  Into) 或者你只是在第一个方法那里设置了一个断点,那么在调试时要进入方法就需要按F5(Step Into)来进入方法. package args参数问题; /* * 思考题1:看程序写结果,然后分析为什么是这个样子的.并画图讲解.最后总结Java中参数传递规律. * Java中的参数传递问题: 基本类型:形式参数的

Java.NIO小例子

--Client.java public class Client { private static String ip = null; private static int port = 10000; public static void main(String[] args) throws Exception{ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); Docum

一个Java线程小例子(仿火车票售卖)

public class MyThread extends Thread{ private static int ticket=100; public void run(){ for(int i=0;i<50;i++) if(ticket>0) System.out.println(Thread.currentThread().getName()+"正在卖第"+(ticket--)+"张票"); } //main函数是一个主线程 public stati

java泛型小例子01

1 class info<T> { 2 private T name; 3 4 info(T name){ 5 this.name = name; 6 } 7 } 8 9 class hello{ 10 11 public static void function(info<?> temp){ 12 System.out.println("内容: "+temp); 13 } 14 15 public static void main(String[] args)

Java一些小例子

package com.example.demo; public class Solution { public static void main(String[] args) { func(); } private static int func() { try { System.out.println("in try"); return func2("try return"); } finally { System.out.println("in fi

java即时通信小例子

学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是在服务上开启一个线程循环等待客户端的连接,为每一个连接到服务器的Socket 创建一个实例存放在集合中,然后使用InputSteam读取从客户端发过来的数据然后转发给除自己之外的所有的客户端 然后是客户端,首先开启一个线程连接都服务器端,循环监听服务器发送回来的数据 这里要说一下为了明确是谁发出的消

java操作xml的一个小例子

最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要定义好bean的层次结构就可以了,第三方包会自动生成和解析xml. 但是今天发现,这个包有两个问题: 一个是比较复杂(我是菜鸟,求别喷,我真的觉得他挺复杂的..).比如说你要想处理既有属性又有值的xml就比较麻烦.(@XStreamConverter注解这种方法报这个错:com.thoughtwor