❶ 用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();
}//这个方法可以不写的,我这是把我自己的工程拷贝过来的
}