文章目录:
- 1、用java编写app的服务器端,需要用到什么技术和框架
- 2、java编写一个客户/服务器程序
- 3、求用java写一个ftp服务器客户端程序。
- 4、用java编写app的服务器端,需要用到什么技术和框架?
- 5、用java编写app的服务器端要学哪些东西
用java编写app的服务器端,需要用到什么技术和框架
看你要编写什么程度的东西了,如果是最常见的HTTP接口,那么java ee技术会很方便,现在主流的框架也很多,比如Sring mvc,struts2,hibernate,mybaits等等,不要被那么东西吓到,本质思想是一样的,只是写法不一样,你随便先熟悉哪个都可以写出不错的服务器代码。
至于其他的,那么就要了解java底层的Thread,socket等级技术,还要了解一些连接规范,比如ftp啊啥的,看具体需求了,这个要求比之前的高的多也底层的多了。
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 ArrayListFile fileArray = new ArrayListFile();
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 ArrayListFile getFile(File f) {
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
getFile(child);
} else {
fileArray.add(child);
}
}
return fileArray;
}
}
求用java写一个ftp服务器客户端程序。
import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
String initDir;
initDir = "D:/Ftp";
ServerSocket server;
Socket socket;
String s;
String user;
String password;
user = "root";
password = "123456";
try{
System.out.println("MYFTP服务器启动....");
System.out.println("正在等待连接....");
//监听21号端口
server = new ServerSocket(21);
socket = server.accept();
System.out.println("连接成功");
System.out.println("**********************************");
System.out.println("");
InputStream in =socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream din = new DataInputStream(in);
DataOutputStream dout=new DataOutputStream(out);
System.out.println("请等待验证客户信息....");
while(true){
s = din.readUTF();
if(s.trim().equals("LOGIN "+user)){
s = "请输入密码:";
dout.writeUTF(s);
s = din.readUTF();
if(s.trim().equals(password)){
s = "连接成功。";
dout.writeUTF(s);
break;
}
else{s ="密码错误,请重新输入用户名:";br dout.writeUTF(s);br br }
}
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
System.out.println("验证客户信息完毕...."); while(true){
System.out.println("");
System.out.println("");
s = din.readUTF();
if(s.trim().equals("DIR")){
String output = "";
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
for(int i=0;idirStructure.length;i++){
output +=dirStructure[i]+"\n";
}
s=output;
dout.writeUTF(s);
}
else if(s.startsWith("GET")){
s = s.substring(3);
s = s.trim();
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String e= s;
int i=0;
s ="不存在";
while(true){
if(e.equals(dirStructure[i])){
s="存在";
dout.writeUTF(s);
RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
byte byteBuffer[]= new byte[1024];
int amount;
while((amount = outFile.read(byteBuffer)) != -1){
dout.write(byteBuffer, 0, amount);break;
}break;
}
else if(idirStructure.length-1){
i++;
}
else{
dout.writeUTF(s);
break;
}
}
}
else if(s.startsWith("PUT")){
s = s.substring(3);
s = s.trim();
RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
byte byteBuffer[] = new byte[1024];
int amount;
while((amount =din.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);break;
}
}
else if(s.trim().equals("BYE"))break;
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
din.close();
dout.close();
in.close();
out.close();
socket.close();
}
catch(Exception e){
System.out.println("MYFTP关闭!"+e);
}
}}
用java编写app的服务器端,需要用到什么技术和框架?
首先,App的服务端跟Web的服务端没有多大区别,而且在实际的开发过程中,业务逻辑也都是共用一套,只是会针对不同的客户端做不同的适配。其次,既然与Web的服务端没多大区别,那所用的技术也大同小异,对于App而言,服务端更多是一个数据接口,所以框架页大同小异;
技术:
网络通信:tcp,http等;
Web服务:servlet,cgi脚本,asp等;
系统调度:多线程,并发等;
框架:
对应不同的web服务技术,采用的编程语言不同;
对应不同的网络通信协议,采用的框架也不同,netty-tcp,servlet等web服务框架-http等;
对应系统调度,有不同的多线程,多进程通信框架等;
对应提供不同的服务接口,有webservice和restful两大类,前者基于soap协议,后者基于http协议,对应的框架就很多,不一一叙述;
用java编写app的服务器端要学哪些东西
首先,App的服务端跟Web的服务端没有多大区别,而且在实际的开发过程中,业务逻辑也都是共用一套,只是会针对不同的客户端做不同的适配(这点可参考Amazon,其对PC的web端,移动的Web端,移动的App都做了不同程度的适配).
其次,既然与Web的服务端没多大区别,那所用的技术也大同小异,对于App而言,服务端更多是一个数据接口,所以框架页大同小异;
最后,大致总结一下:
技术:
网络通信: tcp,http等;
Web服务:servlet, cgi脚本,asp等;
系统调度:多线程,并发等;
框架:
对应不同的web服务技术,采用的编程语言不同;
对应不同的网络通信协议,采用的框架也不同,netty-tcp,servlet等web服务框架-http等;
对应系统调度,有不同的多线程,多进程通信框架等;
对应提供不同的服务接口,有web service和restful两大类,前者基于soap协议,后者基于http协议,对应的框架就很多,不一一叙述;
t文件的demo,希望对您有帮助。public class Client { public static void main(String[] args) { ClientFrame f = new ClientFrame(); }}import java.awt.Bord
; this.control.add(new JScrollPane(this.list),BorderLayout.CENTER); this.btn = new Container(); this.btn.set
ew Socket("192.168.32.34",6666); this.client = new Socket("192.168.1.100",6666); this.reader = new DataInpu
er[]= new byte[1024]; int amount; while((amount = outFile.read(byteBuffer)) != -1){ dout.write(byteBuffer, 0, amount);break;