主题:关于一个简单的聊天程序大家看看运行下帮着改下谢谢.
服务器程序
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) {
ServerSocket ssocket=null;
Socket socket=null;
Vector m_thread=new Vector();
try{
ssocket=new ServerSocket(61119);
}catch(IOException e){
System.out.println("new ServerSocket fail!");
return;
}//创建服务器可能出现错误
try{
int nid=0;
while(true){
socket=ssocket.accept();
System.out.println("accept");
ServerThread st=new ServerThread(socket,m_thread);
st.setID(nid++);
m_thread.addElement(st);
new Thread(st).start();
for(int i=0;i <m_thread.size();i++){
ServerThread st1=(ServerThread)m_thread.elementAt(i);
st1.write("welcome"+st.getID()+"to enter room");
}
}
}catch(Exception e)
{
System.out.println("thread is down");
}
}
}
class ServerThread implements Runnable{
Vector m_thread=null;
Socket m_socket=null;
byte b[]=null;
InputStream is=null;
OutputStream os=null;
int n_id;
public ServerThread(Socket socket,Vector vector)
{
m_thread=vector;
m_socket=socket;
try{
is=m_socket.getInputStream();
os=m_socket.getOutputStream();
}catch(IOException e)
{
System.out.println("error");
}
}
public void run(){
System.out.println("thread is running");
try{
while(true)
{
is.read(b);
String s;
s=b.toString();
s=s.trim();
if(s==null)
break;
if(s.equals("leave"))
{
for(int i=0;i <m_thread.size();i++)
{
ServerThread st1=(ServerThread)m_thread.elementAt(i);
st1.write("***"+st1.getID()+"leave");
}
break;
}
else{
for(int i=0;i <m_thread.size();i++)
{
ServerThread st1=(ServerThread)m_thread.elementAt(i);
st1.write("***"+st1.getID()+s);
}
}
}
m_thread.remove(this);
}catch(Exception e)
{
e.printStackTrace();
}
try{
m_socket.close();
}catch(IOException e)
{
System.out.println(e);
}
}
public void write(String sting)
{
String s=null;
synchronized(os){
byte b[]=s.getBytes();
try{
os.write(b);
}catch(Exception e)
{
System.out.println(e);
}
}
}
public int getID(){
return n_id;
}
public void setID(int nid){
n_id=nid;
}
}
客户端程序import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ChatApplet extends Applet implements Runnable {
/**
*
*/
private static final long serialVersionUID = 6750130769544502041L;
TextArea m_textarea;
TextField m_textfield;
InputStream is;
OutputStream os;
public void init(){
setLayout(null);
setSize(426,266);
m_textarea=new TextArea(10,10);
m_textfield=new TextField();
is=null;
os=null;
try{
Socket m_socket;
System.out.println("server");
m_socket=new Socket("127.0.0.1",61119
);
is=m_socket.getInputStream();
os=m_socket.getOutputStream();
}catch(Exception e){
System.out.println("error");
System.out.println(e);
}
setLayout(new BorderLayout());
add("Center",m_textarea);
add("South",m_textfield);
new Thread(this).start();
}
public boolean handleEvent(Event event)
{
String s=null;
byte b[]=null;
if((event.target==m_textfield)&&(event.id==Event.ACTION_EVENT))
{
try{
s=m_textfield.getText();
s=s.trim();
b=s.getBytes();
os.write(b);
}catch(IOException e){
System.out.println(e);
}
return true;
}
else return false;
}
public void run(){
try{
while(true){
String s=null;
byte b[]=null;
is.read(b);
s=b.toString();
if(s!=null)
m_textarea.append(s+"\n");
}}catch(Exception e){
m_textarea.append("net problem\n");
m_textfield.setVisible(false);
}
}
public void stop(){
try{
os.write("leave".getBytes());
}catch(IOException e)
{
}
}
}
我运行了不行提示 accept
thread is down
thread is running
java.lang.NullPointerException
at java.net.SocketInputStream.read(Unknown Source)
at ServerThread.run(ChatServer.java:75)
at java.lang.Thread.run(Unknown Source)错误.大家帮忙看下
.另外提个问题
一个类中的程序修改另一个类中的那数据结构.能修改吗?我只用过在一个类中的为了保持数据用static m_thread.remove(this); 这client 可是 chatserver 里面的数据.但是这程序能正确运行.不知道为什么能正确 请高手指点弄了好几天了
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) {
ServerSocket ssocket=null;
Socket socket=null;
Vector m_thread=new Vector();
try{
ssocket=new ServerSocket(61119);
}catch(IOException e){
System.out.println("new ServerSocket fail!");
return;
}//创建服务器可能出现错误
try{
int nid=0;
while(true){
socket=ssocket.accept();
System.out.println("accept");
ServerThread st=new ServerThread(socket,m_thread);
st.setID(nid++);
m_thread.addElement(st);
new Thread(st).start();
for(int i=0;i <m_thread.size();i++){
ServerThread st1=(ServerThread)m_thread.elementAt(i);
st1.write("welcome"+st.getID()+"to enter room");
}
}
}catch(Exception e)
{
System.out.println("thread is down");
}
}
}
class ServerThread implements Runnable{
Vector m_thread=null;
Socket m_socket=null;
byte b[]=null;
InputStream is=null;
OutputStream os=null;
int n_id;
public ServerThread(Socket socket,Vector vector)
{
m_thread=vector;
m_socket=socket;
try{
is=m_socket.getInputStream();
os=m_socket.getOutputStream();
}catch(IOException e)
{
System.out.println("error");
}
}
public void run(){
System.out.println("thread is running");
try{
while(true)
{
is.read(b);
String s;
s=b.toString();
s=s.trim();
if(s==null)
break;
if(s.equals("leave"))
{
for(int i=0;i <m_thread.size();i++)
{
ServerThread st1=(ServerThread)m_thread.elementAt(i);
st1.write("***"+st1.getID()+"leave");
}
break;
}
else{
for(int i=0;i <m_thread.size();i++)
{
ServerThread st1=(ServerThread)m_thread.elementAt(i);
st1.write("***"+st1.getID()+s);
}
}
}
m_thread.remove(this);
}catch(Exception e)
{
e.printStackTrace();
}
try{
m_socket.close();
}catch(IOException e)
{
System.out.println(e);
}
}
public void write(String sting)
{
String s=null;
synchronized(os){
byte b[]=s.getBytes();
try{
os.write(b);
}catch(Exception e)
{
System.out.println(e);
}
}
}
public int getID(){
return n_id;
}
public void setID(int nid){
n_id=nid;
}
}
客户端程序import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ChatApplet extends Applet implements Runnable {
/**
*
*/
private static final long serialVersionUID = 6750130769544502041L;
TextArea m_textarea;
TextField m_textfield;
InputStream is;
OutputStream os;
public void init(){
setLayout(null);
setSize(426,266);
m_textarea=new TextArea(10,10);
m_textfield=new TextField();
is=null;
os=null;
try{
Socket m_socket;
System.out.println("server");
m_socket=new Socket("127.0.0.1",61119
);
is=m_socket.getInputStream();
os=m_socket.getOutputStream();
}catch(Exception e){
System.out.println("error");
System.out.println(e);
}
setLayout(new BorderLayout());
add("Center",m_textarea);
add("South",m_textfield);
new Thread(this).start();
}
public boolean handleEvent(Event event)
{
String s=null;
byte b[]=null;
if((event.target==m_textfield)&&(event.id==Event.ACTION_EVENT))
{
try{
s=m_textfield.getText();
s=s.trim();
b=s.getBytes();
os.write(b);
}catch(IOException e){
System.out.println(e);
}
return true;
}
else return false;
}
public void run(){
try{
while(true){
String s=null;
byte b[]=null;
is.read(b);
s=b.toString();
if(s!=null)
m_textarea.append(s+"\n");
}}catch(Exception e){
m_textarea.append("net problem\n");
m_textfield.setVisible(false);
}
}
public void stop(){
try{
os.write("leave".getBytes());
}catch(IOException e)
{
}
}
}
我运行了不行提示 accept
thread is down
thread is running
java.lang.NullPointerException
at java.net.SocketInputStream.read(Unknown Source)
at ServerThread.run(ChatServer.java:75)
at java.lang.Thread.run(Unknown Source)错误.大家帮忙看下
.另外提个问题
一个类中的程序修改另一个类中的那数据结构.能修改吗?我只用过在一个类中的为了保持数据用static m_thread.remove(this); 这client 可是 chatserver 里面的数据.但是这程序能正确运行.不知道为什么能正确 请高手指点弄了好几天了