编写的Chat1.0版本------------------------------
服务器端
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Server extends JFrame{
Container con = null;
JTextArea jta = null;
ServerSocket ss = null;
ArrayList al = new ArrayList();
Server(int port){
try{
ss=new ServerSocket(port);
}
catch(Exception e){
e.printStackTrace();
}
launchFrame();
}
public void severStart(){
while(true){
try{
Socket s = ss.accept();
al.add(new ServerR(s));
jta.append("New Client:"+"\n Ip: "+s.getInetAddress()+":"+s.getPort()+"\n"
+"Clients count:"+al.size()+"\n");
}
catch(Exception e){
e.printStackTrace();
}
}
}
class ServerR implements Runnable{
Socket s = null;
ServerR(Socket s){
this.s = s;
new Thread(this).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
// System.out.println (str);
Iterator ite = al.iterator();
while(ite.hasNext()){
((ServerR)ite.next()).sent(str);
}
str=dis.readUTF();
} }
catch(Exception e){
try{
s.close();
al.remove(this);
jta.append("A client quit!\nClients count:"+al.size()+"\n");
}
catch(Exception e2){
e2.printStackTrace();
}
e.printStackTrace();
}
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
this.setBounds(200,200,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main (String[] args) {
Server s = new Server(3456);
s.severStart();
}
}客户端import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame{
Container con=null;
JTextArea jta = null;
JTextField jtf = null;
Socket s = null;
Client(String ip,int port){
try{
s=new Socket(ip,port);
launchFrame();
}
catch(Exception e){
e.printStackTrace();
}
new Thread(new ClientR()).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void disconnect() throws Exception
{
s.close();
}
class ClientR implements Runnable{
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
System.out.println (str);
jta.append(str+"\n");
str = dis.readUTF();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void startClient(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
while(true){
sent(str);
if(str.equals("q")){
disconnect();
return ;
}
str=br.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jtf = new JTextField();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
con.add(jtf,BorderLayout.SOUTH);
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = jtf.getText();
Client.this.sent(str);
jtf.setText("");
}
}); this.setBounds(200,200,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main (String[] args) {
Client cli = new Client("127.0.0.1",3456);
cli.startClient();
}
}------------------------------------------------------------------------------
Chat2.0版本服务器端//<applet code=Server width=200 height=200></applet>
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;public class Server extends JApplet{
Container con = null;
JTextArea jta = null;
ServerSocket ss = null;
ArrayList al = new ArrayList();
public Server(){
try{
ss=new ServerSocket(3456);
}
catch(Exception e){
e.printStackTrace();
}
launchFrame();
}
public void severStart(){
while(true){
try{
Socket s = ss.accept();
al.add(new ServerR(s));
jta.append("New Client:"+"\n Ip: "+s.getInetAddress()+":"+s.getPort()+"\n"
+"Clients count:"+al.size()+"\n");
}
catch(Exception e){
e.printStackTrace();
}
}
}
class ServerR implements Runnable{
Socket s = null;
ServerR(Socket s){
this.s = s;
new Thread(this).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
// System.out.println (str);
Iterator ite = al.iterator();
while(ite.hasNext()){
((ServerR)ite.next()).sent(str);
}
str=dis.readUTF();
} }
catch(Exception e){
try{
s.close();
al.remove(this);
jta.append("A client quit!\nClients count:"+al.size()+"\n");
}
catch(Exception e2){
e2.printStackTrace();
}
e.printStackTrace();
}
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
this.setBounds(200,200,300,400);
this.setVisible(true);
}
public void init(){
//Server s = new Server(3456);
severStart();
}
// public static void main (String[] args) {
// Server s = new Server(3456);
// s.severStart();
// }
}
客户端//<applet code=Client width=200 height=200></applet>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame{
Container con=null;
JTextArea jta = null;
JTextField jtf = null;
Socket s = null;
public Client(){
try{
s=new Socket("127.0.0.1",3456);
launchFrame();
}
catch(Exception e){
e.printStackTrace();
}
new Thread(new ClientR()).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void disconnect() throws Exception
{
s.close();
}
class ClientR implements Runnable{
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
System.out.println (str);
jta.append(str+"\n");
str = dis.readUTF();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void startClient(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
while(true){
sent(str);
if(str.equals("q")){
disconnect();
return ;
}
str=br.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jtf = new JTextField();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
con.add(jtf,BorderLayout.SOUTH);
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = jtf.getText();
Client.this.sent(str);
jtf.setText("");
}
}); this.setBounds(200,200,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
// public static void main (String[] args) {
// Client cli = new Client("127.0.0.1",3456);
// cli.startClient();
// }
public void init(){
// Client cli = new Client("127.0.0.1",3456);
startClient();
}
}
----------------------------------------------------------------------------
为什么把(Chat1.0)客户端和服务端放入JApplet中(Chat2.0),就无法运行?小弟刚入手java 请大家帮忙看看 谢谢啦~~~~
服务器端
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Server extends JFrame{
Container con = null;
JTextArea jta = null;
ServerSocket ss = null;
ArrayList al = new ArrayList();
Server(int port){
try{
ss=new ServerSocket(port);
}
catch(Exception e){
e.printStackTrace();
}
launchFrame();
}
public void severStart(){
while(true){
try{
Socket s = ss.accept();
al.add(new ServerR(s));
jta.append("New Client:"+"\n Ip: "+s.getInetAddress()+":"+s.getPort()+"\n"
+"Clients count:"+al.size()+"\n");
}
catch(Exception e){
e.printStackTrace();
}
}
}
class ServerR implements Runnable{
Socket s = null;
ServerR(Socket s){
this.s = s;
new Thread(this).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
// System.out.println (str);
Iterator ite = al.iterator();
while(ite.hasNext()){
((ServerR)ite.next()).sent(str);
}
str=dis.readUTF();
} }
catch(Exception e){
try{
s.close();
al.remove(this);
jta.append("A client quit!\nClients count:"+al.size()+"\n");
}
catch(Exception e2){
e2.printStackTrace();
}
e.printStackTrace();
}
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
this.setBounds(200,200,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main (String[] args) {
Server s = new Server(3456);
s.severStart();
}
}客户端import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame{
Container con=null;
JTextArea jta = null;
JTextField jtf = null;
Socket s = null;
Client(String ip,int port){
try{
s=new Socket(ip,port);
launchFrame();
}
catch(Exception e){
e.printStackTrace();
}
new Thread(new ClientR()).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void disconnect() throws Exception
{
s.close();
}
class ClientR implements Runnable{
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
System.out.println (str);
jta.append(str+"\n");
str = dis.readUTF();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void startClient(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
while(true){
sent(str);
if(str.equals("q")){
disconnect();
return ;
}
str=br.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jtf = new JTextField();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
con.add(jtf,BorderLayout.SOUTH);
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = jtf.getText();
Client.this.sent(str);
jtf.setText("");
}
}); this.setBounds(200,200,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main (String[] args) {
Client cli = new Client("127.0.0.1",3456);
cli.startClient();
}
}------------------------------------------------------------------------------
Chat2.0版本服务器端//<applet code=Server width=200 height=200></applet>
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;public class Server extends JApplet{
Container con = null;
JTextArea jta = null;
ServerSocket ss = null;
ArrayList al = new ArrayList();
public Server(){
try{
ss=new ServerSocket(3456);
}
catch(Exception e){
e.printStackTrace();
}
launchFrame();
}
public void severStart(){
while(true){
try{
Socket s = ss.accept();
al.add(new ServerR(s));
jta.append("New Client:"+"\n Ip: "+s.getInetAddress()+":"+s.getPort()+"\n"
+"Clients count:"+al.size()+"\n");
}
catch(Exception e){
e.printStackTrace();
}
}
}
class ServerR implements Runnable{
Socket s = null;
ServerR(Socket s){
this.s = s;
new Thread(this).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
// System.out.println (str);
Iterator ite = al.iterator();
while(ite.hasNext()){
((ServerR)ite.next()).sent(str);
}
str=dis.readUTF();
} }
catch(Exception e){
try{
s.close();
al.remove(this);
jta.append("A client quit!\nClients count:"+al.size()+"\n");
}
catch(Exception e2){
e2.printStackTrace();
}
e.printStackTrace();
}
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
this.setBounds(200,200,300,400);
this.setVisible(true);
}
public void init(){
//Server s = new Server(3456);
severStart();
}
// public static void main (String[] args) {
// Server s = new Server(3456);
// s.severStart();
// }
}
客户端//<applet code=Client width=200 height=200></applet>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame{
Container con=null;
JTextArea jta = null;
JTextField jtf = null;
Socket s = null;
public Client(){
try{
s=new Socket("127.0.0.1",3456);
launchFrame();
}
catch(Exception e){
e.printStackTrace();
}
new Thread(new ClientR()).start();
}
public void sent(String str){
try{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
catch(Exception e){
e.printStackTrace();
}
}
public void disconnect() throws Exception
{
s.close();
}
class ClientR implements Runnable{
public void run(){
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(true){
System.out.println (str);
jta.append(str+"\n");
str = dis.readUTF();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void startClient(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
while(true){
sent(str);
if(str.equals("q")){
disconnect();
return ;
}
str=br.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void launchFrame(){
con = this.getContentPane();
jta = new JTextArea();
jtf = new JTextField();
jta.setEditable(false);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
con.add(jsp,BorderLayout.CENTER);
con.add(jtf,BorderLayout.SOUTH);
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = jtf.getText();
Client.this.sent(str);
jtf.setText("");
}
}); this.setBounds(200,200,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
// public static void main (String[] args) {
// Client cli = new Client("127.0.0.1",3456);
// cli.startClient();
// }
public void init(){
// Client cli = new Client("127.0.0.1",3456);
startClient();
}
}
----------------------------------------------------------------------------
为什么把(Chat1.0)客户端和服务端放入JApplet中(Chat2.0),就无法运行?小弟刚入手java 请大家帮忙看看 谢谢啦~~~~
改为s = new Socket(getDocumentBase().getHost(), 3456);试试。