//==========================================JUI的服务器============================================import java.awt.Dimension;
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;public class Serverl extends JFrame implements Runnable,ActionListener {
Socket s = null;
JButton st;
JButton fs;
TextArea to;
Thread th2;
TextArea sr;
Serverl s1;
public void f(){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - 330)/2);
int y = (int) ((dimension.getHeight() - 210)/2);
this.setLocation(x,y);
((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("2.jpg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(348, 386);
this.setVisible(true);
JPanel jp1 = (JPanel) this.getContentPane();
jp1.setLayout(null);
to = new TextArea();
to.setBounds(9, 60, 334, 168);
this.add(to);
// ta.setEnabled(false);
sr = new TextArea();
sr.setBounds(10, 240, 331, 80);
this.add(sr);
st=new JButton("开启服务器");
st.setBounds(10, 325, 80, 30);
st.setMargin(new Insets(0,0,0,0));
st.addActionListener(this);
this.add(st);
fs=new JButton("发送消息");
fs.setBounds(248, 325, 80, 30);
fs.setMargin(new Insets(0,0,0,0));
fs.addActionListener(this);
this.add(fs);
}
public void run() {
ServerSocket b = null;
try {
b = new ServerSocket(8000);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(true){
s=b.accept();
InputStream in=s.getInputStream();
BufferedReader ber=new BufferedReader(new InputStreamReader(in));
String str=ber.readLine();
to.append("客户端:"+str+"\n");
// System.out.println("客户端:"+str+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "已断开连接!!");
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==st){
Serverl dx=new Serverl();
Thread th=new Thread(dx);
th.start();
}
}
public static void main(String[] args) {
new Serverl().f();
}
}
//=========================================客户端==================================================import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Client {
Socket s;
public void connectClient(){
try {
s = new Socket("localhost",8000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//接收客户端发送的数据
Thread th1 = new Thread(){
public void run(){
while(true){
try {
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
while((str = br.readLine()) != null){
System.out.println("服务器端说:" +str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
th1.start();
Thread th2 = new Thread(){
public void run(){
while(true){
try {
OutputStream os = s.getOutputStream();
PrintStream ps = new PrintStream(os,true);
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
ps.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
th2.start();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client().connectClient();
}}为什么我接受客户端to.append("客户端:"+str+"\n");老报空指针····换来其他的都没问题
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;public class Serverl extends JFrame implements Runnable,ActionListener {
Socket s = null;
JButton st;
JButton fs;
TextArea to;
Thread th2;
TextArea sr;
Serverl s1;
public void f(){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - 330)/2);
int y = (int) ((dimension.getHeight() - 210)/2);
this.setLocation(x,y);
((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("2.jpg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(348, 386);
this.setVisible(true);
JPanel jp1 = (JPanel) this.getContentPane();
jp1.setLayout(null);
to = new TextArea();
to.setBounds(9, 60, 334, 168);
this.add(to);
// ta.setEnabled(false);
sr = new TextArea();
sr.setBounds(10, 240, 331, 80);
this.add(sr);
st=new JButton("开启服务器");
st.setBounds(10, 325, 80, 30);
st.setMargin(new Insets(0,0,0,0));
st.addActionListener(this);
this.add(st);
fs=new JButton("发送消息");
fs.setBounds(248, 325, 80, 30);
fs.setMargin(new Insets(0,0,0,0));
fs.addActionListener(this);
this.add(fs);
}
public void run() {
ServerSocket b = null;
try {
b = new ServerSocket(8000);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(true){
s=b.accept();
InputStream in=s.getInputStream();
BufferedReader ber=new BufferedReader(new InputStreamReader(in));
String str=ber.readLine();
to.append("客户端:"+str+"\n");
// System.out.println("客户端:"+str+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "已断开连接!!");
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==st){
Serverl dx=new Serverl();
Thread th=new Thread(dx);
th.start();
}
}
public static void main(String[] args) {
new Serverl().f();
}
}
//=========================================客户端==================================================import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Client {
Socket s;
public void connectClient(){
try {
s = new Socket("localhost",8000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//接收客户端发送的数据
Thread th1 = new Thread(){
public void run(){
while(true){
try {
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
while((str = br.readLine()) != null){
System.out.println("服务器端说:" +str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
th1.start();
Thread th2 = new Thread(){
public void run(){
while(true){
try {
OutputStream os = s.getOutputStream();
PrintStream ps = new PrintStream(os,true);
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
ps.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
th2.start();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client().connectClient();
}}为什么我接受客户端to.append("客户端:"+str+"\n");老报空指针····换来其他的都没问题
解决方案 »
- 你感觉你是个JAVA高手吗,那么就来挑战一下吧,下面可是ACM一道很经典的题目,试一吧
- 简单提问在线等:类加载
- 如何导入2个java源码?
- 如何在java中调用windows media player 并在客户端显示出来?
- 求java网络及多线程编程的电子书
- MM第一帖,从C#转向Java的第一个困惑!很简单,不许笑!
- java为什么这么简单的方法都没有????我没有找到movefile,copyfile,这些都是最基本的操作!!!!
- 急求《UML和模式应用:面向对象分析与设计导论》中文版
- ant如何能传递程序的命令行参数?
- 麻烦告知小弟那有网络编程的例子,50分相送
- 请教大家关于一个java项目目录划分问题!
- 做了个网站!现在经理想知道来的用户都是从哪里来的!这个。。。
if(e.getSource()==st){
Thread th=new Thread(this);
th.start();
} }