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;
}
}服务端和客户端代码:
public class Main {
public static void main(String[] args) throws Exception {
UDPServer server = new UDPServer();
serverstart(6666, "logs/udpservertxt");
Threadsleep(500);
UDPClient client = new UDPClient();
clientstart(6666);
for(int i = 0; i < 10; i++) {
clientsend("Hello: " + i + "\n");
}
clientstop();
Threadsleep(500);
serverstop();
}
}
class UDPServer implements Runnable{
private DatagramSocket ds;
private boolean running = false;
private Thread thread = null;
private FileOutputStream fos;
public void start(int port, String logFilename) throws Exception{
if(!running) {
running = true;
ds = new DatagramSocket(new InetSocketAddress("127001", 6666));
fos = new FileOutputStream(logFilename);
thread = new Thread(this);
threadstart();
}
}
@Override
public void run() {
while(running) {
try {
byte[] buf = new byte[2048];
DatagramPacket dp = new DatagramPacket(buf, buflength);
dssetSoTimeout(500);
try {
dsreceive(dp);
foswrite(dpgetData(), 0, dpgetLength());
} catch (SocketTimeoutException e) {
}
} catch (IOException e) {
eprintStackTrace();
break;
}
}
try {
fosclose();
} catch (IOException e) {
eprintStackTrace();
}
dsclose();
}
public void stop() {
if(running) {
running = false;
threadinterrupt();
}
}
}
class UDPClient{
private DatagramSocket ds;
public void start(int port) throws Exception{
ds = new DatagramSocket(0);
dsconnect(new InetSocketAddress("127001", 6666));
}
public void send(String message) {
byte[] data = messagegetBytes();
DatagramPacket dp = new DatagramPacket(data, datalength);
try {
dssend(dp);
} catch (IOException e) {
eprintStackTrace();
}
}
public void stop() {
if (ds != null) {
dsclose();
}
}
}
界面代码你自己写吧我想你应该是想问C/S架构中的客户端和服务端
两者最常见的方式是通过Socket方式进行通信。
Socket可以理解成一个电线插座的工作过程:
服务器是电源插件, 客户端是电器
C和S通过电线和指定的插孔进行连接,连上后,S将电力源源不断发送到C, C就可以工作了。 当然C也可以反向发送信息到S。 两者可以相互通信。
在建立的过程中代码有一些不同。
在服务端采用API类是ServerSocket
在客户端采用的API是Socket类
连接建立后,双方都通过连接获取输入和输出流从而实现通信。即: InputStream is=socketgetInputStream();
isread();
连接代码:
S端:
ServerSocket server=null;
try {
server=new ServerSocket(指定的端口);
}catch(Exception e){
Systemoutprintln("不能监听:"+etoString());
}
Socket socket=null;
try {
socket=serveraccept();
InputStream is=socketgetInputStream();
//己通过建立起流,可以读取客户端发来的请求了
//同样也可以发送能过 sokcetgetOutputStream()
}
catch(IOException e){
Systemoutprintln("出错:"+etoString());
}finally{
try {
if(socket!=null){
socketclose();
serverclose();
}
}
catch(IOException e){
eprintStackTrace();
}
}
客户端:
Socket socket=null;
try {
socket=new Socket(url,端口);
//获取输出流,从而向服务端发数据
socketgetOutputStream();
//获取输入流,从而可以读服务端的数据
socketgetInputStream();
}catch(Exception e){
eprintStackTrace();
}
finally{
try {
socketclose();
}
catch(IOException e){
eprintStackTrace();
}
}先要给楼主说一下:
rmi,ejb主要应用在b/s程序中,当然如果楼主一定要用也可以。
如果楼主需要开发一个c/s程序的话,使用swing技术就可以了,您在学习swing的时候可以使用很多中ide插件,如myeclipse55ga,可以很方便的为您提供一套ide系统,用起来就像vb一样,通过拖拽控件和监听自动生成代码。
另外,您说进销存服装管理系统肯定是要和数据库挂钩的,所以您需要学习jdbc,也就是说,swing是做界面和写业务逻辑的,而jdbc是链接数据库的。
如果做您说的那个功能,学这两个足够了!
不过说实话,这样的系统如果使用delphi的话速度不知要比java快多少倍!
不过也要看楼主老板或者客户的要求了,谁让人家给钱啊哈哈。
祝好运
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)