新手,请各位帮忙看个ServerSocket的程序,谢谢

jackbaby 2006-02-09 01:18:26
服务器端

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Server
{
public Server()
{
ServerService MyServer=new ServerService(6544,10);
}


}

class ServerService extends Frame //服务器端的监听窗口
{
ServerSocket m_sListener; //监听器
public ServerService(int Port,int Count)
{
try
{
m_sListener=new ServerSocket(6544,10);//建立监听服务
while(true)
{
Socket Connected=m_sListener.accept();//接受来自Client端的请求
ServiceThread MyST=new ServiceThread(this,Connected);
MyST.ConnectThread.start();
}
}
catch(IOException e){}//异常处理方法
}
}


class ServiceThread extends Frame implements Runnable
{//当CLIENT有请求时 SERVER端创建一个FRAME用于与之交互数据
ServerService FatherListener;
Socket ConnectedClient;
Thread ConnectThread;
public ServiceThread(ServerService sv,Socket s)//构造函数
{
FatherListener=sv;
ConnectedClient=s;
ConnectThread=new Thread(this);
}

public void run()
{
try{
DataInputStream in=new DataInputStream(//获得从CILENT读入的数据流
new BufferedInputStream(ConnectedClient.getInputStream()));
PrintStream out=new PrintStream(//获得向CILENT输出的数据流
new BufferedOutputStream(ConnectedClient.getOutputStream()));
String s=in.readLine();//从CLIENT端读入信息
System.out.println(s);
}
catch(Exception e){}
//FatherListener.addMeg("Client"+"closed."+"\n");
dispose();
}
}


---------

public class Bank {

public static void main(String[] args) {

Server server = new Server();
}
}


客户端

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

/**
* Sample application using Frame.
*
* @author
* @version 1.00 06/02/07
*/
public class ClientFrame extends JFrame implements ActionListener{

private JMenu JWithdraw = new JMenu("WithDraw");
private JMenu JDisposit = new JMenu("Disposit");
private JMenu JBalance = new JMenu("Balance");
private JMenu JExit = new JMenu("Exit");

//
private JTextField jtid,jtpass,jtamount,jtbalance,jtText;


//
Socket ClientSocket;
PrintStream os;
DataInputStream is;
String s;

private JButton JBWithdraw,JBDispose,JBCheck,JBClear,JBExit;
public ClientFrame()
{
setSize(600,250);
/*
*JMenu
*/
JMenuBar Face = new JMenuBar();

setJMenuBar(Face);


JWithdraw.setMnemonic('W');
Face.add(JWithdraw);


JDisposit.setMnemonic('W');
Face.add(JDisposit);


JBalance.setMnemonic('W');
Face.add(JBalance);


JExit.setMnemonic('W');
Face.add(JExit);

JPanel p = new JPanel();
p.setLayout(new GridLayout(1,1));


JPanel top = new JPanel();
top.setLayout(new FlowLayout(FlowLayout.CENTER));
top.add(new JLabel("Dave's Bank Automatic Telling System"));

p.add(top);

JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(7,2));


JPanel ID = new JPanel();
ID.setLayout(new FlowLayout(FlowLayout.RIGHT));
ID.add(new JLabel("Account Number:"));
JPanel IDT = new JPanel();
IDT.setLayout(new FlowLayout(FlowLayout.LEFT));
IDT.add(jtid=new JTextField(10));

JPanel Pass = new JPanel();
Pass.setLayout(new FlowLayout(FlowLayout.RIGHT));
Pass.add(new JLabel("PassWord:"));
JPanel PassT = new JPanel();
PassT.setLayout(new FlowLayout(FlowLayout.LEFT));
PassT.add(jtpass=new JTextField(10));

JPanel Amount = new JPanel();
Amount.setLayout(new FlowLayout(FlowLayout.RIGHT));
Amount.add(new JLabel("Amount:"));
JPanel AmountT = new JPanel();
AmountT.setLayout(new FlowLayout(FlowLayout.LEFT));
AmountT.add(jtamount=new JTextField(10));

JPanel Balance = new JPanel();
Balance.setLayout(new FlowLayout(FlowLayout.RIGHT));
Balance.add(new JLabel("Balance:"));
JPanel BalanceT = new JPanel();
BalanceT.setLayout(new FlowLayout(FlowLayout.LEFT));
BalanceT.add(jtbalance=new JTextField(10));





// p1.add(top1);
// p1.add(top);
p1.add(ID);
p1.add(IDT);
p1.add(Pass);
p1.add(PassT);
p1.add(Amount);
p1.add(AmountT);
p1.add(Balance);
p1.add(BalanceT);

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,1));

JPanel Text = new JPanel();
Text.setLayout(new FlowLayout(FlowLayout.LEFT));
Text.add(jtText=new JTextField(10));

p2.add(Text);

JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1,5));


p3.add(JBWithdraw=new JButton("Withdraw"));
p3.add(JBDispose=new JButton("Dispose"));
p3.add(JBCheck=new JButton("Check"));
p3.add(JBClear=new JButton("Clear"));
p3.add(JBExit=new JButton("Exit"));

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.CENTER);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.CENTER);
getContentPane().add(p3,BorderLayout.CENTER);

getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.WEST);
getContentPane().add(p3,BorderLayout.SOUTH);

JBExit.addActionListener(this);
JBWithdraw.addActionListener(this);

}

// Handle ActionEvent from buttons and menu items
public void actionPerformed(ActionEvent e)
{


String actionCommand = e.getActionCommand();

System.out.println(actionCommand);
if (e.getSource()==this.JBExit)
{
System.exit(0);
}
if (e.getSource()==this.JBWithdraw)
{
connect();
}
}

public void connect()
{
try
{
ClientSocket=new Socket("localhost",6544);
os=new PrintStream(
new BufferedOutputStream(ClientSocket.getOutputStream()));
is=new DataInputStream(
new BufferedInputStream(ClientSocket.getInputStream()));
s=is.readLine();
jtid.;
}
catch(Exception e)
{
}
}
}


------------

public class Client{

public Client()
{
super();
}

public static void main(String[] args) throws Exception{
// Create application frame.
ClientFrame f = new ClientFrame();

// Show frame
f.setVisible(true);
}
}


我想获得每个JTextField的值(在服务器端),谢谢各位了,谢谢
...全文
106 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackbaby 2006-02-09
  • 打赏
  • 举报
回复
新手,请大家帮忙,谢谢

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧