❶ 用java 寫出一個Server 和Client 能實現Client發送的消息顯示在服務端上,Server發送的信息顯示在客戶端上
你好!
我寫的很簡單,你可以在此基礎上完善,好了,廢話不多說,直入正題。
首先,建立一個項目名為TestChat,然後建兩個包,分別為server和client。
server包下建立一個類為Server,一個類為ServerListen。
client包建立一個類Client,一個類為ClentListen。
由於我時間倉促,你自行參考哈。
***********************************************************************
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Server.java
*
* Created on 2011-5-8, 16:37:13
*/
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Study
*/
public class Server extends javax.swing.JFrame {
/** Creates new form Server */
public Server() {
initComponents();
try {
ss = new ServerSocket(8888);
s = ss.accept();
in = new ObjectInputStream(getS().getInputStream());
out = new ObjectOutputStream(getS().getOutputStream());
this.startListen();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
messageShow = new javax.swing.JTextArea();
messageInput = new javax.swing.JTextField();
sendBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("伺服器");
messageShow.setColumns(20);
messageShow.setEditable(false);
messageShow.setRows(5);
jScrollPane1.setViewportView(messageShow);
sendBtn.setText("發送");
sendBtn.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
sendBtnMousePressed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addComponent(messageInput, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(333, Short.MAX_VALUE)
.addComponent(sendBtn)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(messageInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(sendBtn)
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void sendBtnMousePressed(java.awt.event.MouseEvent evt) {
this.sendMessage();
this.messageInput.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Server().setVisible(true);
}
});
}
// Variables declaration - do not modify
protected javax.swing.JScrollPane jScrollPane1;
protected javax.swing.JTextField messageInput;
protected javax.swing.JTextArea messageShow;
protected javax.swing.JButton sendBtn;
// End of variables declaration
private ServerSocket ss;
private Socket s;
private ObjectOutputStream out;
private ObjectInputStream in;
private void startListen() {
ServerListen listen = new ServerListen(this);
Thread t = new Thread(listen);
t.start();
}
/**
* @return the s
*/
public Socket getS() {
return s;
}
/**
* @return the out
*/
public ObjectOutputStream getOut() {
return out;
}
/**
* @return the in
*/
public ObjectInputStream getIn() {
return in;
}
/**
* @return the messageShow
*/
public javax.swing.JTextArea getMessageShow() {
return messageShow;
}
private void sendMessage() {
try {
out.writeObject("伺服器說");
out.flush();
out.writeObject(this.messageInput.getText().trim());
out.flush();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
❷ java怎麼創建hivemetastoreclient
下面就來解釋下系統是如何生成meta client的!
先來看幾段代碼!
publicvoidcreateDatabase(Databasedb,booleanifNotExist)throwsAlreadyExistsException,HiveException{
try{
getMSC().createDatabase(db);
}catch(AlreadyExistsExceptione){
if(!ifNotExist){
throwe;
}
}catch(Exceptione){
thrownewHiveException(e);
}
}
=========
privateIMetaStoreClientgetMSC()throwsMetaException{
if(metaStoreClient==null){
metaStoreClient=createMetaStoreClient();
}
returnmetaStoreClient;
}
=========
()throwsMetaException{
HiveMetaHookLoaderhookLoader=newHiveMetaHookLoader(){
publicHiveMetaHookgetHook(org.apache.hadoop.hive.metastore.api.Tabletbl)throwsMetaException{
try{
if(tbl==null){
returnnull;
}
=HiveUtils.getStorageHandler(conf,
tbl.getParameters().get(META_TABLE_STORAGE));
if(storageHandler==null){
returnnull;
}
returnstorageHandler.getMetaHook();
}catch(HiveExceptionex){
LOG.error(StringUtils.stringifyException(ex));
thrownewMetaException("Failedtoloadstoragehandler:"+ex.getMessage());
}
}
};
returnnewHiveMetaStoreClient(conf,hookLoader);
}
=========
publicHiveMetaStoreClient(HiveConfconf,HiveMetaHookLoaderhookLoader)
throwsMetaException{
this.hookLoader=hookLoader;
if(conf==null){
conf=newHiveConf(HiveMetaStoreClient.class);
}
this.conf=conf;
localMetaStore=conf.getBoolVar(ConfVars.METASTORE_MODE);
if(localMetaStore){
//
//throughthenetwork
client=newHiveMetaStore.HMSHandler("hiveclient",conf);
isConnected=true;
return;
}
//getthenumberretries
retries=HiveConf.getIntVar(conf,HiveConf.ConfVars.METASTORETHRIFTRETRIES);
retryDelaySeconds=conf.getIntVar(ConfVars.METASTORE_CLIENT_CONNECT_RETRY_DELAY);
//
if(conf.getVar(HiveConf.ConfVars.METASTOREURIS)!=null){
StringmetastoreUrisString[]=conf.getVar(
HiveConf.ConfVars.METASTOREURIS).split(",");
metastoreUris=newURI[metastoreUrisString.length];
try{
inti=0;
for(Strings:metastoreUrisString){
URItmpUri=newURI(s);
if(tmpUri.getScheme()==null){
("URI:"+s
+"doesnothaveascheme");
}
metastoreUris[i++]=tmpUri;
}
}catch(IllegalArgumentExceptione){
throw(e);
}catch(Exceptione){
MetaStoreUtils.logAndThrowMetaException(e);
}
}elseif(conf.getVar(HiveConf.ConfVars.METASTOREDIRECTORY)!=null){
metastoreUris=newURI[1];
try{
metastoreUris[0]=newURI(conf
.getVar(HiveConf.ConfVars.METASTOREDIRECTORY));
}catch(URISyntaxExceptione){
MetaStoreUtils.logAndThrowMetaException(e);
}
}else{
LOG.error("NOTgettingurisfromconf");
thrownewMetaException("");
}
//finallyopenthestore
open();
}
下面要認真分析下上面的這段代碼,因為關聯到一些參數的配置,對於理解生產環境的部署參數有幫助!先看下面這段代碼
localMetaStore=conf.getBoolVar(ConfVars.METASTORE_MODE);
if(localMetaStore){
//
//connecting
//throughthenetwork
client=newHiveMetaStore.HMSHandler("hiveclient",conf);
isConnected=true;
return;
}
PS:ConfVars.METASTORE_MODE---METASTORE_MODE("hive.metastore.local",true),
❸ 有關java client——server 間傳送文件的問題,本想通過server把client1 發來的信息傳送給client2
client必須保持長連接,
簡單說,你的client和server之間的連接是不能斷開的,你每次啟動client都是內直接嘗試讀取然後斷掉鏈接容的話,
c1在傳輸文件的時候,c2的連接已經斷掉了,server還如何和c2之間通訊呢?
❹ 求問 java中 client 和 implementer分別是什麼 區別是什麼謝謝
client? 這不是客戶端嗎?
在java中基於b/s架構的 client是瀏覽器
而基於 c/s 和三層 c/s 的 client是由程序員自己專門開發的一個軟體,用於支持業務的顯示(給用戶用的界面,他可以用別的語言來編寫 如 java , c,c++等)
implementer?沒見過
implement 介面
介面就是定義了一系列的方法,介面不能直接實例化,只能被實現
介面我舉個例子,就像電腦 有 usb介面 你可以插 USB鍵盤,然後通過鍵盤來控制電腦
如:
public interface EmployeeDAO
{//這個是介面的定義,你懂的
public List<Employee> findAll() throws Exception;
public void delete(int id) throws Exception ;
public void save(Employee e) throws Exception ;
public Employee findById(int id) throws Exception;
public void modify(Employee e) throws Exception;
}
public class EmployeeJDBCImpl implements EmployeeDAO
{//這是實現介面,裡面要不介面中定義的方法全都寫出來,這里我就寫一個啊throws 這是拋出異常,你可以不寫的,前提是定義介面時的方法別寫throws Exception
public List<Employee> findAll() throws Exception
{
return null;
}
public class ActionServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// EmployeeDAO = (EmployeeDAO) Factor.getInstance("EmployeeDAO"); 這個不用管
EmployeeDAO = (EmployeeDAO) new EmployeeJDBCImpl();//這個你懂的
List<Employee> employees = .findAll();
}//這個方法可以不寫的,我這是把我自己的工程拷貝過來的
}