public class Client {
public static void main(String[] args) {
ClientFrame f = new ClientFrame();
}
}
import javaawtBorderLayout;
import javaawtContainer;
import javaawtDimension;
import javaawtGridLayout;
import javaawtToolkit;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaioDataInputStream;
import javaioDataOutputStream;
import javaioIOException;
import javanetSocket;
import javanetUnknownHostException;
import javautilVector;
import javaxswingBorderFactory;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJList;
import javaxswingJScrollPane;
import javaxswingJTextArea;
import javaxswingeventListSelectionEvent;
import javaxswingeventListSelectionListener;
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(){
thislist = new JList();
thislistsetBorder(BorderFactorycreateTitledBorder("文件列表"));
thislistaddListSelectionListener(this);
thissbtn = new JButton("显示");
thissbtnaddActionListener(this);
thiscbtn = new JButton("清除");
thiscbtnaddActionListener(this);
thiscontrol = new Container();
thiscontrolsetPreferredSize(new Dimension(150, 400));
thiscontrolsetLayout(new BorderLayout());
thiscontroladd(new JScrollPane(thislist),BorderLayoutCENTER);
thisbtn = new Container();
thisbtnsetLayout(new GridLayout(1,2));
btnadd(sbtn);
btnadd(cbtn);
thiscontroladd(thisbtn,BorderLayoutSOUTH);
thistxt = new JTextArea();
thistxtsetEditable(false);
thistxtsetSize(350, 400);
thissetTitle("客户端");
thissetSize(500, 400);
thissetVisible(true);
Dimension displaySize = ToolkitgetDefaultToolkit()getScreenSize();
thissetLocation((displaySizewidth - thisgetWidth()) / 2, (displaySizeheight - thisgetHeight()) / 2);
thissetLayout(new BorderLayout());
thisadd(thiscontrol,BorderLayoutWEST);
thisadd(new JScrollPane(thistxt),BorderLayoutCENTER);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
try {
//thisclient = new Socket("1921683234",6666);
thisclient = new Socket("1921681100",6666);
thisreader = new DataInputStream(clientgetInputStream());
thiswriter = new DataOutputStream(clientgetOutputStream());
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
public void actionPerformed(ActionEvent event){
if(eventgetSource() == sbtn){
if(v == null){
v = new Vector();
}
else{
vclear();
}
try {
writerwriteUTF("getfilelist");
writerflush();
String t = readerreadUTF();
while( t != null && !tequals("")){
vadd(t);
t = readerreadUTF();
}
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
thislistsetListData(v);
}
if(eventgetSource() == cbtn){
thistxtsetText("");
}
}
public void valueChanged(ListSelectionEvent e) {
int i = thislistgetSelectedIndex();
if (! thislistgetValueIsAdjusting() && i != -1) {
try {
writerwriteUTF("getfilecontent_" + i);
writerflush();
String tmp = readerreadUTF();
thistxtsetText(tmp);
} catch (IOException e1) {
e1printStackTrace();
}
}
}
}
import javaioDataInputStream;
import javaioDataOutputStream;
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStreamReader;
import javaioReader;
import javanetServerSocket;
import javanetSocket;
import javautilArrayList;
import javautilIterator;
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 = serveraccept();
DataInputStream reader = new DataInputStream(clientgetInputStream());
DataOutputStream writer = new DataOutputStream(clientgetOutputStream());
while(true){
cmd = readerreadUTF();
Systemoutprintln(cmd);
if(cmdequals("getfilelist")){
fileArrayclear();
//fileArray = getFile(new File("D:/tmp"));
fileArray = getFile(new File("D:/学习/教程/学习笔记"));
String fn = "";
for(int k = 0; k < fileArraysize(); k ++){
fn = fileArrayget(k)getName();
writerwriteUTF(fn);
writerflush();
}
writerwriteUTF("");
}
if(cmdstartsWith("getfilecontent_")){
int i = IntegerparseInt(cmdsplit("_")[1]);
File f = fileArrayget(i);
Reader in = new InputStreamReader(new FileInputStream(f));
int tempbyte;
String str = "";
while ((tempbyte = inread()) != -1) {
str += (char)tempbyte;
//Systemoutprintln(str);
}
inclose();
writerwriteUTF(str);
}
}
} catch (IOException e) {
eprintStackTrace();
}
}
private static ArrayList<File> getFile(File f) {
File[] ff = flistFiles();
for (File child : ff) {
if (childisDirectory()) {
getFile(child);
} else {
fileArrayadd(child);
}
}
return fileArray;
}
}ServerSocket。
“java程序中使用tcp套接字编写服务端程序的套接字类是”是《Java语言》复习资料的一道练习题,答案是ServerSocket。
Java是一门编程语言,是一门连接人与计算机的语言。首先,App的服务端跟Web的服务端没有多大区别,而且在实际的开发过程中,业务逻辑也都是共用一套,只是会针对不同的客户端做不同的适配(这点可参考Amazon,其对PC的web端,移动的Web端,移动的App都做了不同程度的适配)
其次,既然与Web的服务端没多大区别,那所用的技术也大同小异,对于App而言,服务端更多是一个数据接口,所以框架页大同小异;
最后,大致总结一下:
技术:
网络通信: tcp,>
服务端:
ServerSocket serverSocket = new ServerSocket(2008);while (true) {
Socket socket = serverSocketaccept();
MyThread thread = new MyThread(socket);
threadstart();
}public class MyThread extends Thread {
private Socket socket;
public MyThread(Socket socket) {
thissocket = socket;
}
@Override
public void run() {
superrun();
try {
InputStream is = socketgetInputStream();
BufferedReader bufReader = new BufferedReader(new InputStreamReader(is));
String line = "";
String s = "";
while ((s = bufReaderreadLine()) != null) {
line = line + s;
}
Systemoutprintln("接收到:" + line);
} catch (IOException e) {
eprintStackTrace();
}
}
}
客户端:
Socket socket=new Socket("1921680101", 2008);
OutputStream out=socketgetOutputStream();
PrintWriter pw=new PrintWriter(new OutputStreamWriter(out));
pwprint("12345上山打老虎");
pwflush();
pwclose();
outclose();
}两者都可以不过java的垃圾回收机制因为是自动的所以执行客户端程序的时候效率特别慢一般用于服务器端或WEB开发学java的重点就是以j2ee技术为核心的。想写客户端之类的用C语言是最佳的不过内存处理机制全部自己编写难度不是一般的高。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)