Server.java
创新互联公司长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为源汇企业提供专业的成都网站建设、网站制作,源汇网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private ListServerThread serverThreads;
private ServerSocket serverSocket;
public Server() throws IOException {
serverSocket = new ServerSocket(8025);
serverThreads = new ArrayListServerThread();
}
public ListServerThread getServerThreads() {
return serverThreads;
}
public void setServerThreads(ListServerThread serverThreads) {
this.serverThreads = serverThreads;
}
public ServerSocket getServerSocket() {
return serverSocket;
}
public void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public static void main(String[] args) {
Server server = null;
try {
server = new Server();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
try {
Socket socket;
Thread t;
while (true) {
socket = server.getServerSocket().accept();
ServerThread serverThread = new ServerThread(server, socket);
server.getServerThreads().add(serverThread);
t = new Thread(serverThread);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message, Socket socket) {
for (ServerThread serverThread : serverThreads) {
try {
serverThread.getDos().writeUTF(message);
} catch (IOException e) {
// e.printStackTrace();
System.out.println("推送消息给" + serverThread.getSocket().getRemoteSocketAddress() + "过程中发生了错误.");
}
}
}
}
class ServerThread implements Runnable {
private Server server;
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
public ServerThread(Server server, Socket socket) throws IOException {
this.server = server;
this.socket = socket;
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public void run() {
String message = "";
while (!message.equals("logout")) {
try {
message = socket.getRemoteSocketAddress() + " say: " + dis.readUTF();
// System.out.println(message);
server.sendMessage(message, socket);
} catch (IOException e) {
//e.printStackTrace();
break;
}
}
server.getServerThreads().remove(this);
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
public Client() throws UnknownHostException, IOException {
socket = new Socket("127.0.0.1", 8025);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
new Thread(new ClientThread(this)).start();
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public void close() {
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessage(String message) {
try {
dos.writeUTF(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = null;
try {
client = new Client();
System.out.println(client.getSocket().getLocalPort());
} catch (IOException e) {
System.out.println("BUG");
e.printStackTrace();
System.exit(-1);
}
String message = "";
Scanner scanner = new Scanner(System.in);
while (!message.equals("logout")) {
message = scanner.nextLine();
// System.out.println(message);
client.sendMessage(message);
}
scanner.close();
client.close();
}
}
class ClientThread implements Runnable {
private Client client;
public ClientThread(Client client) {
super();
this.client = client;
}
@Override
public void run() {
try {
while (true) {
System.out.println(client.getDis().readUTF());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
mport java.awt.HeadlessException;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;@SuppressWarnings("serial")public class MainFrame extends JFrame {JLabel lbl1 = new JLabel("用户名:");JLabel lbl2 = new JLabel("密 码:");JTextField txt = new JTextField("admin",20);JPasswordField pwd = new JPasswordField(20);JButton btn = new JButton("登录");JPanel pnl = new JPanel();private int error = 0;public MainFrame(String title) throws HeadlessException {super(title);init();}private void init() {this.setResizable(false);pwd.setEchoChar('*');pnl.add(lbl1);pnl.add(txt);pnl.add(lbl2);pnl.add(pwd);pnl.add(btn);this.getContentPane().add(pnl);btn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if ("admin".equals(new String(pwd.getPassword()))){pnl.removeAll();JLabel lbl3 = new JLabel();ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));lbl3.setIcon(icon);pnl.add(lbl3);}else{if(error 3){JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次");error++;}else{JOptionPane.showMessageDialog(null,"对不起,您不是合法用户");txt.setEnabled(false);pwd.setEnabled(false);btn.setEnabled(false);}}}});}public static void main(String[] args) {MainFrame frm = new MainFrame("测试");frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frm.setBounds(100, 100, 300, 120);frm.setVisible(true);}}
我这里有一个例子希望能够帮助你
public class Client{
private Socket socket;
try{
Socket socket=new Socket ("localhoast',8088);
ip=InetAddress.getLocalHost();
String localip=ip.getHostAddress();
System.out.println(localip);
String name=ip.getHostName();
System.out.println(name);
}
catch(Exception e){
}
public void start(){
try{
Run r2=new Run();
Thread t1=new Thread(r2);
t1.setDaemon(true);
t1.start();
Scanner sc=new Scanner(System.in);
OutputStream os=socket.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
PrintWriter writer=new PrintWriter(osw,true);
while(true){
writer.println(sc.nextLine());
}
}catch(Exception e){
}
}
public static void main(String args[]){
Client client=new Client();
client.start();
}
class Run implements Runnable{
public void run() {
while(true){
try {
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
while(true){
String str=br.readLine();
System.out.println("服务器说:"+str);
if("bye".equals(str)){
System.out.println("再见客户端");
System.out.println("聊天结束");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
在用到web service时,如果是调用自己开发的web service还好,可以有一个方法的接口或参数对象实体类什么的,但如果是调用第三方的服务,除了得到web service服务地址的wsdl文档外,没有任何实质性编码的东西,写原生调用太麻烦,这时就需要自己写一个方法的接口类。
这个,目前已经有比较成熟的工具,可以自动生成,下面作一下简单的介绍。
apache的wsdl2java工具,目前貌似CXF和axis各有一套,使用方式大同小异,但生成的代码会有所区别,这时说的是CXF提供的wsdl2java工具。
wsdl2java用法:
wsdl2java -p com -d src -all aa.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
详细用法见:
一开始使用了CXF的这套工具,发现不是很方便,毕竟依赖CXF提供的工具jar包,个人不是很喜欢用。
后来发现JDK居然也自带了对web service生成java代码的功能,貌似1.6版本开始的,试用后发现效果十分不错,果断投入它的怀抱。下面作下简单的介绍,以备忘。
打开jdk下的bin目录 看下能否找到"wsimport.exe"这个文件
一般情况下都会有
如果没有则说明你的JDK不支持这个功能
然后在DOS窗口下输入wsimport 敲回车
如果提示错误的话 说明你的JDK环境变量还没有配好
一句话总结,如果你在dos窗口下输入wsimport正常,就可以
如果OK的话 输入以下命令就可以将wsdl文件生成java文件了
wsimport -keep -p com.llg.ws2 -s g:/ws
参数说明
wsimport 这个是必须的 该工具的名称
wsdl文件
-keep 是否生成源文件
-p com.llg.ws2 生成后的java包名
-s g:/ws 生成后放哪个目录
但是前几天,在对一个第三方提供的web service使用该工具生成java代码的时候,居然出错了,网上查找后终于找到原因。
使用cxf wsdl2java或javax wsimport工具的时候,可能会遇到关于生成的Response类文件名冲突的问题
Console代码
WSDLToJava Error: Thrown by JAXB : A class/interface with the same name "***" is already in use. Use a class customization to resolve this conflict.
目前可选择的方案:
apache的wsdl2java工具,使用-autoNameResolution自动处理
wsdl2java -autoNameResolution
JDK自带的工具
wsimport -p com.test.client -keep -B-XautoNameResolution
2.如果web service是己方开发的,可以修改代码,使用自定义bindings,详细可看sun webservice文档
例如:
Java代码
public interface ValidateCCService
@WebMethod
@WebResult(name = "response")
public ValidateCCResponse validateCC(@WebParam(name = "request")ValidateCCRequest request);
这个情况下定义的方法名,如果使用工具生成客户端代码,很可能存在Response冲突,因为定义的wsdl中会有一个关于接口方法的message
wsdl:message name="validateCCResponse"
/wsdl:message
方法名定义的message与接口定义返回的ValidateCCResponse,在工具生成客户端代码时就会产生命名冲突。
但是通过更改接口方法名为:
Java代码
public interface ValidateCCService
@WebMethod
@WebResult(name = "response")
public ValidateCCResponse validate(@WebParam(name = "request")ValidateCCRequest request);
即可解决该冲突,