1、Java用来编写客户端还是服务器端?
两者都可以...不过java的垃圾回收机制因为是自动的所以执行客户端程序的时候效率特别慢...一般用于服务器端或WEB开发...学java的重点就是以j2ee技术为核心的。想写客户端之类的用C语言是最佳的..不过内存处理机制全部自己编写..难度不是一般的高。
2、如何用java编写服务器端和客户端
B
3、如何用Java实现Web服务器
如何用Java实现Web服务器 一、HTTP协议的作用原理
WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页。WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间采用超文本传送协议(HTTP)进行通信。HTTP协议是基于TCP/IP协议之上的协议,是Web浏览器和Web服务器之间的应用层协议,是通用的、无状态的、面向对象的协议。HTTP协议的作用原理包括四个步骤:
(1) 连接:Web浏览器与Web服务器建立连接,打开一个称为socket(套接字)的虚拟文件,此文件的建立标志着连接建立成功。
(2) 请求:Web浏览器通过socket向Web服务器提交请求。HTTP的请求一般是GET或POST命令(POST用于FORM参数的传递)。GET命令的格式为:
GET 路径/文件名 HTTP/1.0
文件名指出所访问的文件,HTTP/1.0指出Web浏览器使用的HTTP版本。
(3) 应答:Web浏览器提交请求后,通过HTTP协议传送给Web服务器。Web服务器接到后,进行事务处理,处理结果又通过HTTP传回给Web浏览器,从而在Web浏览器上显示出所请求的页面。
4、用JAVA编写一个客户机服务器聊天程序
服务器端(注意要先启动服务器端)
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class server extends Frame implements ActionListener {
Label label = new Label("交谈内容");
Panel panel = new Panel();
TextField tf = new TextField(10);
TextArea ta = new TextArea();
ServerSocket server;
Socket client;
InputStream in;
OutputStream out;
public server() {
super("服务器");
setSize(250, 250);
panel.add(label);
panel.add(tf);
tf.addActionListener(this);
add("North", panel);
add("Center", ta);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
show();
try {
server = new ServerSocket(4000);
client = server.accept();
ta.append("客户机是:" + client.getInetAddress().getHostName() + "\n\n");
in =client.getInputStream();
out= client.getOutputStream();
} catch (IOException ioe) {
}
while (true) {
try {
byte[] buf = new byte[256];
in.read(buf);
String str = new String(buf);
ta.append("客户机说:" + str + "\n\n");
} catch (IOException e) {
}
}
}
public void actionPerformed(ActionEvent e) {
try {
String str = tf.getText();
byte[] buf = str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:" + str + "\n");
} catch (IOException ioe) {
}
}
public static void main(String[] args) {
new server();
}
}
客户端
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class client extends Frame implements ActionListener {
Label label = new Label("交谈内容");
Panel panel = new Panel();
TextField tf = new TextField(10);
TextArea ta = new TextArea();
Socket client;
InputStream in;
OutputStream out;
public client() {
super("客户机");
setSize(250, 250);
panel.add(label);
panel.add(tf);
tf.addActionListener(this);
add("North", panel);
add("Center", ta);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
show();
try {
client = new Socket(InetAddress.getLocalHost(), 4000);
ta.append("服务器是:" + client.getInetAddress().getHostName() + "\n\n");
in = client.getInputStream();
out = client.getOutputStream();
} catch (IOException ioe) {
}
while (true) {
try {
byte[] buf = new byte[256];
in.read(buf);
String str = new String(buf);
ta.append("服务器说:" + str + "\n");
} catch (IOException e) {
}
}
}
public void actionPerformed(ActionEvent e) {
try {
String str = tf.getText();
byte[] buf = str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:" + str + "\n");
} catch (IOException iOE) {
}
}
public static void main(String args[]) {
new client();
}
}
这个只能在自己一台电脑上先启动服务器再启动客户端才行,要想一台机子启动服务器端一台机子启动客户端需要把客户端的 client = new Socket(InetAddress.getLocalHost(), 4000);改成 client = new Socket("服务器Ip", 4000);(前提是两台机子连在局域网里面的)
5、如何用Java实现Web服务器
如何用Java实现Web服务器 一、HTTP协议的作用原理
WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页。WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间采用超文本传送协议(HTTP)进行通信。HTTP协议是基于TCP/IP协议之上的协议,是Web浏览器和Web服务器之间的应用层协议,是通用的、无状态的、面向对象的协议。HTTP协议的作用原理包括四个步骤:
(1) 连接:Web浏览器与Web服务器建立连接,打开一个称为socket(套接字)的虚拟文件,此文件的建立标志着连接建立成功。
(2) 请求:Web浏览器通过socket向Web服务器提交请求。HTTP的请求一般是GET或POST命令(POST用于FORM参数的传递)。GET命令的格式为:
GET 路径/文件名 HTTP/1.0
文件名指出所访问的文件,HTTP/1.0指出Web浏览器使用的HTTP版本。
(3) 应答:Web浏览器提交请求后,通过HTTP协议传送给Web服务器。Web服务器接到后,进行事务处理,处理结果又通过HTTP传回给Web浏览器,从而在Web浏览器上显示出所请求的页面。
6、java编写一个客户/服务器程序
下面是我自己写的一个读取并显示txt文件的demo,希望对您有帮助。
public class Client {
public static void main(String[] args) {
ClientFrame f = new ClientFrame();
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ClientFrame extends JFrame implements ActionListener, ListSelectionListener{
private JList list = null;
private JButton sbtn = null;
private JButton cbtn = null;
private Vector v = null;
private JTextArea txt = null;
private Container control = null;
private Container btn = null;
private Socket client = null;
private DataInputStream reader = null;
private DataOutputStream writer = null;
public ClientFrame(){
this.list = new JList();
this.list.setBorder(BorderFactory.createTitledBorder("文件列表"));
this.list.addListSelectionListener(this);
this.sbtn = new JButton("显示");
this.sbtn.addActionListener(this);
this.cbtn = new JButton("清除");
this.cbtn.addActionListener(this);
this.control = new Container();
this.control.setPreferredSize(new Dimension(150, 400));
this.control.setLayout(new BorderLayout());
this.control.add(new JScrollPane(this.list),BorderLayout.CENTER);
this.btn = new Container();
this.btn.setLayout(new GridLayout(1,2));
btn.add(sbtn);
btn.add(cbtn);
this.control.add(this.btn,BorderLayout.SOUTH);
this.txt = new JTextArea();
this.txt.setEditable(false);
this.txt.setSize(350, 400);
this.setTitle("客户端");
this.setSize(500, 400);
this.setVisible(true);
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((displaySize.width - this.getWidth()) / 2, (displaySize.height - this.getHeight()) / 2);
this.setLayout(new BorderLayout());
this.add(this.control,BorderLayout.WEST);
this.add(new JScrollPane(this.txt),BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
//this.client = new Socket("192.168.32.34",6666);
this.client = new Socket("192.168.1.100",6666);
this.reader = new DataInputStream(client.getInputStream());
this.writer = new DataOutputStream(client.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == sbtn){
if(v == null){
v = new Vector();
}
else{
v.clear();
}
try {
writer.writeUTF("getfilelist");
writer.flush();
String t = reader.readUTF();
while( t != null && !t.equals("")){
v.add(t);
t = reader.readUTF();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.list.setListData(v);
}
if(event.getSource() == cbtn){
this.txt.setText("");
}
}
public void valueChanged(ListSelectionEvent e) {
int i = this.list.getSelectedIndex();
if (! this.list.getValueIsAdjusting() && i != -1) {
try {
writer.writeUTF("getfilecontent_" + i);
writer.flush();
String tmp = reader.readUTF();
this.txt.setText(tmp);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
public class Server {
static ArrayList<File> fileArray = new ArrayList<File>();
public static void main(String args[]) {
ServerSocket server = null;
Socket client = null;
String cmd = "";
try {
server = new ServerSocket(6666);
client = server.accept();
DataInputStream reader = new DataInputStream(client.getInputStream());
DataOutputStream writer = new DataOutputStream(client.getOutputStream());
while(true){
cmd = reader.readUTF();
System.out.println(cmd);
if(cmd.equals("getfilelist")){
fileArray.clear();
//fileArray = getFile(new File("D:/tmp"));
fileArray = getFile(new File("D:/学习/教程/学习笔记"));
String fn = "";
for(int k = 0; k < fileArray.size(); k ++){
fn = fileArray.get(k).getName();
writer.writeUTF(fn);
writer.flush();
}
writer.writeUTF("");
}
if(cmd.startsWith("getfilecontent_")){
int i = Integer.parseInt(cmd.split("_")[1]);
File f = fileArray.get(i);
Reader in = new InputStreamReader(new FileInputStream(f));
int tempbyte;
String str = "";
while ((tempbyte = in.read()) != -1) {
str += (char)tempbyte;
//System.out.println(str);
}
in.close();
writer.writeUTF(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static ArrayList<File> getFile(File f) {
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
getFile(child);
} else {
fileArray.add(child);
}
}
return fileArray;
}
}
7、用java编写一个获取时间的服务器
/**
* Created by IntelliJ IDEA.
* User: Knight_Sun
* Date: 2008-5-20
* Time: 9:19:09
* To change this template use File | Settings | File Templates.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
public class Showtime extends JFrame implements ActionListener {
private JButton timeBox;
private JTextField timeField;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日 a h时m分s秒");
public Showtime(String title) throws HeadlessException {
super(title);
init();
doLay();
handleEvents();
}
private void init() {
timeBox = new JButton("刷新时间");
timeField = new JTextField(20);
}
private void doLay() {
Container container = getContentPane();
container.add(timeBox, BorderLayout.NORTH);
container.add(timeField, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void handleEvents() {
timeBox.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Timestamp nowtime=new Timestamp(System.currentTimeMillis());
timeField.setText(dateFormat.format(nowtime.getTime()));
}
public static void main(String[] args) {
new Showtime("Show Time");
}
}
看看是不是你要的效果,一个按钮,点一下就显示最新时间.
8、用Java编写创建一对客户端/服务器程序,利用数据报将一个文件从一台主机传送到另一
下面是我自己写的一个读取并显示txt文件的demo,希望对您有帮助。
public class Client {
public static void main(String[] args) {
ClientFrame f = new ClientFrame();
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ClientFrame extends JFrame implements ActionListener, ListSelectionListener{
private JList list = null;
private JButton sbtn = null;
private JButton cbtn = null;
private Vector v = null;
private JTextArea txt = null;
private Container control = null;
private Container btn = null;
private Socket client = null;
private DataInputStream reader = null;
private DataOutputStream writer = null;
public ClientFrame(){
this.list = new JList();
this.list.setBorder(BorderFactory.createTitledBorder("文件列表"));
this.list.addListSelectionListener(this);
this.sbtn = new JButton("显示");
this.sbtn.addActionListener(this);
this.cbtn = new JButton("清除");
this.cbtn.addActionListener(this);
this.control = new Container();
this.control.setPreferredSize(new Dimension(150, 400));
this.control.setLayout(new BorderLayout());
this.control.add(new JScrollPane(this.list),BorderLayout.CENTER);
this.btn = new Container();
this.btn.setLayout(new GridLayout(1,2));
btn.add(sbtn);
btn.add(cbtn);
this.control.add(this.btn,BorderLayout.SOUTH);
this.txt = new JTextArea();
this.txt.setEditable(false);
this.txt.setSize(350, 400);
this.setTitle("客户端");
this.setSize(500, 400);
this.setVisible(true);
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((displaySize.width - this.getWidth()) / 2, (displaySize.height - this.getHeight()) / 2);
this.setLayout(new BorderLayout());
this.add(this.control,BorderLayout.WEST);
this.add(new JScrollPane(this.txt),BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
//this.client = new Socket("192.168.32.34",6666);
this.client = new Socket("192.168.1.100",6666);
this.reader = new DataInputStream(client.getInputStream());
this.writer = new DataOutputStream(client.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == sbtn){
if(v == null){
v = new Vector();
}
else{
v.clear();
}
try {
writer.writeUTF("getfilelist");
writer.flush();
String t = reader.readUTF();
while( t != null && !t.equals("")){
v.add(t);
t = reader.readUTF();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.list.setListData(v);
}
if(event.getSource() == cbtn){
this.txt.setText("");
}
}
public void valueChanged(ListSelectionEvent e) {
int i = this.list.getSelectedIndex();
if (! this.list.getValueIsAdjusting() && i != -1) {
try {
writer.writeUTF("getfilecontent_" + i);
writer.flush();
String tmp = reader.readUTF();
this.txt.setText(tmp);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
public class Server {
static ArrayList<File> fileArray = new ArrayList<File>();
public static void main(String args[]) {
ServerSocket server = null;
Socket client = null;
String cmd = "";
try {
server = new ServerSocket(6666);
client = server.accept();
DataInputStream reader = new DataInputStream(client.getInputStream());
DataOutputStream writer = new DataOutputStream(client.getOutputStream());
while(true){
cmd = reader.readUTF();
System.out.println(cmd);
if(cmd.equals("getfilelist")){
fileArray.clear();
//fileArray = getFile(new File("D:/tmp"));
fileArray = getFile(new File("D:/学习/教程/学习笔记"));
String fn = "";
for(int k = 0; k < fileArray.size(); k ++){
fn = fileArray.get(k).getName();
writer.writeUTF(fn);
writer.flush();
}
writer.writeUTF("");
}
if(cmd.startsWith("getfilecontent_")){
int i = Integer.parseInt(cmd.split("_")[1]);
File f = fileArray.get(i);
Reader in = new InputStreamReader(new FileInputStream(f));
int tempbyte;
String str = "";
while ((tempbyte = in.read()) != -1) {
str += (char)tempbyte;
//System.out.println(str);
}
in.close();
writer.writeUTF(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static ArrayList<File> getFile(File f) {
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
getFile(child);
} else {
fileArray.add(child);
}
}
return fileArray;
}
}
9、java编服务器与客户端之间的一个程序(要求如下)
import java.net.Socket;
import java.io.*;
public class Client{
private Socket socket=null;
private BufferedReader br;
private PrintWriter pw;
private boolean flag=true;
public Client(){
socket=new Socket("localhost",8000);
br=socket.getInputStream();
String str=br.readLine();
while(!str.equals("")&&flag){
System.out.println("Server:"+str);
InputStream is=new InuptStream(System.in);
BufferedReader br1=new BufferedReader(is);
String message=br1.readLine();
OutputStream os=socket.getOutputStream();
pw=new PrintWriter(os);
if(!message.equals("")&&!message.equals("bye"))
pw.println(message);
else
flag=false;
}
br.close();
pw.close();
}
public static void main(String[] args){
new Client();
}
}
//服务器程序
import java.io.*;
import java.net.*;
public class Server{
private ServerSocket ss;
private BufferedReader br;
private PrintWriter pw;
private boolean flag=true;
public Server(){
ss=new ServerSocket(8000);
Socket s=ss.getSocket();
br=s.getInputStream();
pw=s.getOutputStream();
String message="";
message=br.readLine();
while(flag&&message.equals("")){
pw.println(Please enter your name);
pw.println("echo="+message);
if(message.equals("bye"))
flag=false;
}
}
}