我试过了 可以具体一点吗? 我修改的 public static String str1; public final static int DEFAULT_PORT=1027; public MultiUser(Socket c){ this.client=c; } public String getStr() { return str1; } public void run(){ try{ InetAddress ia=client.getInetAddress(); UserIp=ia.getHostAddress(); BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out=new PrintWriter(client.getOutputStream()); while(true){ String str=in.readLine(); System.out.println("Message form IP:"+UserIp+" "+str); str1=str; //out.println(str1); out.flush(); if(str.equals("end")) break; } client.close(); }catch(IOException ex){ } finally{
} } }import java.net.*; import java.io.*;public class tserver { public final static int DEFAULT_PORT=1027; public final static int THREAD_COUNT=100; public static Socket[] st; public static void main(String[] args)throws IOException { String hostname; int port=DEFAULT_PORT; if(args.length>0) { hostname=args[0]; } else { hostname="LocalHost";
可以具体一点吗?
我修改的
public static String str1;
public final static int DEFAULT_PORT=1027;
public MultiUser(Socket c){
this.client=c;
}
public String getStr()
{
return str1;
}
public void run(){
try{
InetAddress ia=client.getInetAddress();
UserIp=ia.getHostAddress();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println("Message form IP:"+UserIp+" "+str);
str1=str;
//out.println(str1);
out.flush();
if(str.equals("end"))
break;
}
client.close();
}catch(IOException ex){
}
finally{
}
}
}import java.net.*;
import java.io.*;public class tserver
{
public final static int DEFAULT_PORT=1027;
public final static int THREAD_COUNT=100;
public static Socket[] st;
public static void main(String[] args)throws IOException
{
String hostname;
int port=DEFAULT_PORT;
if(args.length>0)
{
hostname=args[0];
}
else
{
hostname="LocalHost";
}
if(args.length>1)
{
try
{
port=Integer.parseInt(args[1]);
}
catch(NumberFormatException e)
{
}
}
ServerSocket server=new ServerSocket(1026);
st=new Socket[THREAD_COUNT];
while(true)
{
MultiUser[] user=new MultiUser[THREAD_COUNT];
for(int i=0;i<user.length;i++)
{
st[i]=server.accept();
user[i]=new MultiUser(st[i]);
user[i].start();
System.out.println(user[i].getStr());
}
}
}
}
结果只是从服务器返回到写入服务器的那个线程中,无法在所有的线程中都实现写入。
还是不知道怎么实现。
你可以这样做,在客户端建立一个Timer循环,时间自定义。用自定义的时间向服务器端发送信号。然后当服务器端接收到信号的时候,将聊天的内容返回给客户端。然后客户端进行接受、显示就可以了!(还要代码吗?)
* ChatApp
* for chat with the server
*/ class ChatApp extends JPanel implements ActionListener
{
Timer t=new Timer(5000,new TimerAction());
String usr_name;
public String remUser;
class TimerAction implements ActionListener
{
Socket toServer;
ObjectInputStream streamFromServer;
PrintStream streamToServer;
public void actionPerformed(ActionEvent e2)
{
try
{
toServer=new Socket(getHostName(),8007);
streamFromServer=new ObjectInputStream(toServer.getInputStream());
streamToServer=new PrintStream(toServer.getOutputStream());
message=txtMsg.getText(); //send a message to the server
streamToServer.println("From Timer"); //receive vectors from the server
Vector vector=(Vector)streamFromServer.readObject();
Vector vector1=(Vector)streamFromServer.readObject(); //show the online users
txtListUsers.setText("");
for(int j=1;j<vector1.capacity();j++)
{
txtListUsers.append((String)vector1.elementAt(j));
txtListUsers.append("\n");
} //show the message
int i=messageCount;
for(;i<vector.capacity();i++)
{
txtMessages.append((String)vector.elementAt(i));
txtMessages.append("\n");
}
messageCount=i;
}//end of try
catch(Exception e)
{
System.out.println("Exception "+e);
}
}//end of actionPerformed
}//end of TimerListener
int messageCount=0;
String name;
PrintStream streamToServer;
ObjectInputStream streamFromServer;
Socket toServer; JTextArea txtMessages;
JTextArea txtListUsers;
JTextField txtMsg; JPanel btnPanel;
JButton sendButton; JScrollPane jspTxtMsgPane;
JScrollPane jspUserListPane; String message;
//ChatApp类的构造函数 public ChatApp(String str_name)
{
remUser=str_name;
usr_name=str_name; txtMessages=new JTextArea();
jspTxtMsgPane=new JScrollPane(txtMessages,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); txtListUsers=new JTextArea();
jspUserListPane=new JScrollPane(txtListUsers,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); btnPanel=new JPanel();
txtMsg=new JTextField(20); sendButton=new JButton("发送");
sendButton.addActionListener(this); btnPanel.setLayout(new FlowLayout());
btnPanel.add(txtMsg);
btnPanel.add(sendButton); setLayout(new BorderLayout(5,5));
add("Center",jspTxtMsgPane);
add("East",jspUserListPane);
add("South",btnPanel); t.start();
}//end of ChatApp() public void actionPerformed(ActionEvent e1)
{
if(e1.getSource()==sendButton)
{
try
{
toServer=new Socket("fgx",8007);
streamFromServer=new ObjectInputStream(toServer.getInputStream());
streamToServer=new PrintStream(toServer.getOutputStream());
message=txtMsg.getText();
String msg=message; //send the username and msg type to the server
streamToServer.println(usr_name+":"+msg);
txtMsg.setText(""); //read the reply from the server
Vector vector=(Vector)streamFromServer.readObject();
int i =messageCount;
for(;i<vector.capacity();i++)
{
//display the messages
txtMessages.append((String)vector.elementAt(i));
txtMessages.append("\n");
} messageCount=i;
}//end of try
catch(Exception e)
{
System.out.println("Exception Occurred:"+e);
}
}//end of if
}//end of actionPerformed() //获得服务器名
String getHostName()
{
FileReader fileRead;
BufferedReader buffer=null;
String strHost=null;
try
{
fileRead=new FileReader("hostName.ini");
buffer=new BufferedReader(fileRead);
strHost=buffer.readLine();
}
catch(Exception e)
{
System.out.println("Read file error");
}
return(strHost);
}
};//end of class ChatApp
很谢谢你,看你的名字,你以前是学c++的吧.
其实我也是c++的程序员,大概有三年了吧,但是现在必须学习java.
学了大概一个星期了,也知道了一些东西了.但是还有很多的不知道.能不能谈你学java的经验啊.
上面的程序,怎么没有服务器端的啊.我现在主要是服务器端程序有问题.你那个只是加入了swing开发了一个界面,还是没有实现我要求的,你也是用一个时钟Timer的循环.我也试过这个方法,是可以行的通,不过我还是不知道怎么把服务器端的话发回给全部线程?