详见代码,结果如下:
正在监听Exception in thread "main" java.lang.NullPointerException
at Server.main(Server.java:22)
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class Server
{
public static void main(String args[])
{
ServerSocket server=null;
ServerThread thread;
Socket you=null;
while(true)
{
try{
server=new ServerSocket(4331);//创建在端口4331上负责监听的 ServerSocket对象。
}
catch(IOException e1)
{
System.out.println("正在监听");
}
try{
you=server.accept();// server返回和客户端相连接的Socket对象。
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{
System.out.println("正在等待客户");
}
if(you!=null)
{
new ServerThread(you).start(); //为每个客户启动一个专门的线程。
}
else
{
continue;
}
}
}
}
class ServerThread extends Thread
{
Socket socket;
ObjectInputStream in=null;
ObjectOutputStream out=null;
String s=null;
ServerThread(Socket t)
{
socket=t;
try {
out=new ObjectOutputStream(socket.getOutputStream()); //socket返回输出流。
in=new ObjectInputStream(socket.getInputStream()); //socket返回输入流。
}
catch (IOException e)
{}
}
public void run()
{
TextArea text=new TextArea("你好,我是服务器",12,12);
try
{
out.writeBytes(text.getText());
out.flush();
//out写对象text到客户端。
}
catch (IOException e)
{
System.out.println("客户离开");
}
}
}
另外,附上客户端代码:import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Client extends Frame implements Runnable,ActionListener
{
Button connection;
Socket socket=null;
ObjectInputStream in=null;
ObjectOutputStream out=null;
Thread thread;
public Client()
{
socket=new Socket();
connection=new Button("连接服务器,读取文本区对象");
add(connection,BorderLayout.NORTH);
connection.addActionListener(this);
thread = new Thread(this);
setBounds(100,100,360,310);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void run()
{
while(true)
{
try{
TextArea text=(TextArea)in.readObject();
add(text,BorderLayout.CENTER);
validate();
}
catch(Exception e)
{
break;
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==connection)
{
try
{
if(socket.isConnected())
{
}
else
{
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4331);//创建端口为4331、地址为
//address的socketAddress
socket.connect(socketAddress) ;//socket建立和socketAddress的连接呼叫。
in =new ObjectInputStream(socket.getInputStream()); //socket返回输入流。
out = new ObjectOutputStream(socket.getOutputStream()); //socket返回输出流。
thread.start();
}
}
catch (Exception ee){}
}
}
public static void main(String args[])
{
Client win=new Client();
}
}
正在监听Exception in thread "main" java.lang.NullPointerException
at Server.main(Server.java:22)
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class Server
{
public static void main(String args[])
{
ServerSocket server=null;
ServerThread thread;
Socket you=null;
while(true)
{
try{
server=new ServerSocket(4331);//创建在端口4331上负责监听的 ServerSocket对象。
}
catch(IOException e1)
{
System.out.println("正在监听");
}
try{
you=server.accept();// server返回和客户端相连接的Socket对象。
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{
System.out.println("正在等待客户");
}
if(you!=null)
{
new ServerThread(you).start(); //为每个客户启动一个专门的线程。
}
else
{
continue;
}
}
}
}
class ServerThread extends Thread
{
Socket socket;
ObjectInputStream in=null;
ObjectOutputStream out=null;
String s=null;
ServerThread(Socket t)
{
socket=t;
try {
out=new ObjectOutputStream(socket.getOutputStream()); //socket返回输出流。
in=new ObjectInputStream(socket.getInputStream()); //socket返回输入流。
}
catch (IOException e)
{}
}
public void run()
{
TextArea text=new TextArea("你好,我是服务器",12,12);
try
{
out.writeBytes(text.getText());
out.flush();
//out写对象text到客户端。
}
catch (IOException e)
{
System.out.println("客户离开");
}
}
}
另外,附上客户端代码:import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Client extends Frame implements Runnable,ActionListener
{
Button connection;
Socket socket=null;
ObjectInputStream in=null;
ObjectOutputStream out=null;
Thread thread;
public Client()
{
socket=new Socket();
connection=new Button("连接服务器,读取文本区对象");
add(connection,BorderLayout.NORTH);
connection.addActionListener(this);
thread = new Thread(this);
setBounds(100,100,360,310);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void run()
{
while(true)
{
try{
TextArea text=(TextArea)in.readObject();
add(text,BorderLayout.CENTER);
validate();
}
catch(Exception e)
{
break;
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==connection)
{
try
{
if(socket.isConnected())
{
}
else
{
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4331);//创建端口为4331、地址为
//address的socketAddress
socket.connect(socketAddress) ;//socket建立和socketAddress的连接呼叫。
in =new ObjectInputStream(socket.getInputStream()); //socket返回输入流。
out = new ObjectOutputStream(socket.getOutputStream()); //socket返回输出流。
thread.start();
}
}
catch (Exception ee){}
}
}
public static void main(String args[])
{
Client win=new Client();
}
}
应该是返回的对象是空值, 你没做判断就使用了 所以出现空指针。
if(obj!=null)
{
}
判断一下 跟踪一下。
server=new ServerSocket(4331);这句话出错 你再它的catch中打印一下异常信息就知道是什么错误了 ,一般catch块不要这么写啊 最好打印一个异常信息,不然即使处理了也不知道是什么错误啊
out.flush();
//out写对象text到客户端。
这两句应该有错,如果用正确的语句利用out写对象text到客户端?
"out写对象text到客户端",这个怎么写?