⑴ java中用socket實現客戶端與服務端雙向連接問題
//服務端程序:
importjava.io.*;
importjava.net.*;
publicclassTCPServer{
publicstaticvoidmain(String[]args)throwsIOException{
().init();
}
@SuppressWarnings("static-access")
privatevoidinit()throwsIOException{
@SuppressWarnings("resource")
ServerSocketserver=newServerSocket(1000);
Socketclient=null;
while(true){
try{
client=server.accept();
BufferedInputStreambis=newBufferedInputStream(client.getInputStream());
byte[]b=newbyte[1024];
intlen=0;
Stringmessage="";
while((len=bis.read(b))!=-1){
message=newString(b,0,len);
System.out.print("客戶端:"+client.getInetAddress().getLocalHost().getHostAddress()+"發來消息:"+message);
if("byte".equals(message.trim()))
client.close();
PrintWriterpw=newPrintWriter(client.getOutputStream(),true);
pw.println(message);
}
}catch(Exceptione){
System.err.println("客戶端:"+client.getInetAddress().getLocalHost().getHostAddress()+"已斷開連接!");
}
}
}
}
//客戶端程序:
importjava.io.*;
importjava.net.Socket;
{
publicstaticvoidmain(String[]args)throwsIOException{
newTCPClient().init();
}
privatevoidinit()throwsIOException{
@SuppressWarnings("resource")
finalSocketclient=newSocket("127.0.0.1",1000);
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
Stringsend="";
while(true){
send=in.readLine();
PrintWriterout=newPrintWriter(client.getOutputStream(),true);
if(!"byte".equals(send.trim()))
out.println(send);
else{
out.println(send);
System.exit(0);
}
newThread(newTCPClient(){
@SuppressWarnings("static-access")
publicvoidrun(){
try{
BufferedInputStreambis=newBufferedInputStream(client.getInputStream());
byte[]b=newbyte[1024];
intlen=0;
while((len=bis.read(b))!=-1){
System.out.println("伺服器:"+client.getInetAddress().getLocalHost().getHostAddress()+"發來消息:"+newString(b,0,len).trim());
}
}catch(IOExceptione){
System.err.println("連接伺服器失敗!");
}
}
}).start();
}
}
publicvoidrun(){}
}
//伺服器測試結果:
客戶端:192.168.0.200發來消息:001 byte
客戶端:192.168.0.200發來消息:byte
客戶端:192.168.0.200 已斷開連接!
客戶端:192.168.0.200發來消息:adasd
客戶端:192.168.0.200 已斷開連接!
//客戶端測試結果:
---001號客戶端--
001byte
伺服器:192.168.0.200發來消息:001byte
byte //001禮貌說跟伺服器說byte
---002號客戶端--
adasd //002客戶端直接關閉程序
伺服器:192.168.0.200發來消息:adasd
⑵ java緗戠粶緙栫▼搴旇ユ庢牱鍦ㄥ㈡埛絝鍜屾湇鍔″櫒闂村疄鐜伴氫俊錛
浠ュ墠鍐欑殑錛岀収璐翠簡銆傘傘傛湇鍔″櫒絝錛歩mport java.awt.*;x0dx0aimport java.awt.event.WindowAdapter;x0dx0aimport java.awt.event.WindowEvent;x0dx0aimport java.io.*;x0dx0aimport java.net.*;/*6銆 閲囩敤UDP鍗忚錛岀紪鍐欎竴涓狫ava緗戠粶搴旂敤紼嬪簭錛岃ュ簲鐢ㄥ垎鏈嶅姟鍣ㄧ紼嬪簭鍜屽㈡埛絝紼嬪簭涓ら儴鍒嗐俓x0dx0a* 瀹㈡埛絝鎸囧畾涓涓鏈嶅姟鍣ㄤ笂鐨勬枃浠跺悕錛岃╂湇鍔″櫒鍙戝洖璇ユ枃浠剁殑鍐呭癸紝鎴栬呮彁紺烘枃浠朵笉瀛樺湪銆俓x0dx0a* 錛20鍒嗭級錛堟湇鍔$紼嬪簭鍜屽㈡埛絝紼嬪簭鍒嗗埆鍛藉悕涓篠erver.java鍜孋lient.java錛*/x0dx0apublic class N4BT6 extends Framex0dx0a{x0dx0aDatagramSocket socket ;x0dx0aDatagramPacket packet ;byte[] buf ;x0dx0aFile file ;x0dx0aFileInputStream input;x0dx0aString message = "璇ユ枃浠朵笉瀛樺湪";x0dx0aTextArea text;x0dx0apublic N4BT6(String title)x0dx0a{x0dx0asuper(title);x0dx0atext = new TextArea(6,4);x0dx0aadd(text);x0dx0asetSize(400, 300);x0dx0asetVisible(true);x0dx0aaddWindowListener(new WindowAdapter()x0dx0a{x0dx0apublic void windowClosing(WindowEvent e)x0dx0a{x0dx0adispose();x0dx0a}x0dx0a});x0dx0ax0dx0abuf = new byte[1024];x0dx0atryx0dx0a{x0dx0asocket = new DatagramSocket(1230);x0dx0apacket = new DatagramPacket(buf, buf.length);x0dx0asocket.receive(packet);x0dx0afile = new File(new String(packet.getData()));x0dx0asocket = new DatagramSocket();x0dx0a} x0dx0acatch (Exception e)x0dx0a{e.printStackTrace();x0dx0a}x0dx0ax0dx0aif(file.exists())x0dx0a{x0dx0atryx0dx0a{x0dx0abuf = new byte[(int)file.length()];x0dx0apacket = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),1234);x0dx0ainput = new FileInputStream(file);x0dx0ainput.read(buf);x0dx0asocket.send(packet);x0dx0a}x0dx0acatch (IOException e) x0dx0a{x0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0aelsex0dx0a{x0dx0atryx0dx0a{x0dx0apacket = new DatagramPacket(message.getBytes(),message.getBytes().length,x0dx0aInetAddress.getLocalHost(),1234);x0dx0asocket.send(packet);x0dx0a}x0dx0acatch (Exception e) x0dx0a{x0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0a}x0dx0apublic static void main(String[] args)x0dx0a{x0dx0anew N4BT6("Server");x0dx0a}x0dx0a}x0dx0a瀹㈡埛絝錛歩mport java.awt.*;x0dx0aimport java.awt.event.*;x0dx0aimport java.net.DatagramPacket;x0dx0aimport java.net.DatagramSocket;x0dx0aimport java.net.InetAddress;public class N4BT6_2 extends Framex0dx0a{x0dx0aTextArea text;x0dx0aString message = "Q.txt";x0dx0aDatagramSocket socket ;x0dx0aDatagramPacket packet;x0dx0abyte[] buf;x0dx0apublic N4BT6_2(String title)x0dx0a{x0dx0asuper(title);x0dx0atext = new TextArea(6,4);x0dx0aadd(text);x0dx0asetSize(400, 300);x0dx0asetVisible(true);x0dx0aaddWindowListener(new WindowAdapter()x0dx0a{x0dx0apublic void windowClosing(WindowEvent e)x0dx0a{x0dx0adispose();x0dx0a}x0dx0a});x0dx0atryx0dx0a{x0dx0ax0dx0asocket = new DatagramSocket();x0dx0apacket = new DatagramPacket(message.getBytes(),message.getBytes().length,x0dx0aInetAddress.getLocalHost(),1230);x0dx0asocket.send(packet);x0dx0a}x0dx0acatch (Exception e) x0dx0a{x0dx0ae.printStackTrace();x0dx0a}x0dx0ax0dx0atryx0dx0a{x0dx0abuf = new byte[1024];x0dx0asocket = new DatagramSocket(1234);x0dx0apacket = new DatagramPacket(buf,buf.length);x0dx0asocket.receive(packet);x0dx0atext.append(new String(buf));x0dx0a}x0dx0acatch (Exception e) x0dx0a{x0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0apublic static void main(String[] args)x0dx0a{x0dx0anew N4BT6_2("Client");x0dx0a}x0dx0a}
⑶ 關於Webservice介面的Java客戶端調用
String endpoint="http://localhost:8080/xxx/services/userservice?wsdl";
String id = "11111";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName("webservice方法名");
String res = (String) call.invoke(new Object[] {id});
看了你的描述覺得你把webservice想得太復雜化了,其實就是一個jar包和幾個類。
以上就是最簡單的webservice客戶端用法,和反射有點像。當然返回值不一定是String,返回的類型和格式要問服務提供方。
我用的是axis的,我不了解websphere什麼的,但是webservice就是那麼易用的東西。
⑷ Thrift教程(Java客戶端和伺服器)
在實現Java客戶端與服務端通信時,首先需要編寫Thrift文件來定義介面和結構。
步驟一:創建一個名為'tutorial.thrift'的文件,用於描述服務的介面和數據類型。
接著,運行Thrift編譯器,通過命令生成Java代碼。這通常包括客戶端和服務端的介面實現。
步驟二:在新創建的Maven項目中操作,將thrift編譯後的'gen_java'目錄下的Java代碼,如介面類和數據類,復制到src/main/java目錄下。
然後,開始編寫Java客戶端代碼,這將調用在步驟一中生成的介面來與服務端交互。
在客戶端,你將看到如下輸出,表示請求和響應的處理過程:
[客戶端輸出]
與此同時,在服務端,你需要實現相應的服務介面,處理客戶端的請求:
[服務端輸入]
最後,項目的目錄結構應包含Maven的基本結構,以及從Thrift生成的Java代碼所在的文件夾。
完整的項目結構如下:
[項目目錄結構描述]