[英]Error in creating a Server side Chat program in JAVA?
所以,我正在嘗試創建一個客戶端服務器聊天程序。 因為,我沒有外部服務器,所以我的客戶端和服務器將托管在我的計算機中。 現在,我正在關注在線教程並創建了聊天程序的服務器端,但是當我測試程序的服務器端時出現錯誤。 當我創建服務器套接字時,如果我正在使用我的計算機,我應該輸入什么端口號。 我使用了一個隨機數,但出現以下錯誤:-有人可以幫我修復錯誤或建議我該怎么做嗎? 謝謝
這是我的服務器類:-
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Server extends JFrame {
private JTextField usertext;
private JTextArea chatwindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public Server()
{
super("Missy's Instant Messenger");//This sets the title of the messenger window
usertext = new JTextField(); //this is for creating the textfield where the user will enter data
usertext.setEditable(false);//this is set to false such that the user can only send message if he is connected to someone
usertext.addActionListener( //this activity is for sending the message when the user clicks on enter
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
usertext.setText(" ");
}
/* private void sendMessage(String string) {
// TODO Auto-generated method stub
}*/
});
add(usertext,BorderLayout.NORTH);
chatwindow = new JTextArea();
add(new JScrollPane(chatwindow));
setSize(500,500);
}
public void startrunning(){
try {
server = new ServerSocket(6800,100);
//6789 is the port number and 100 is the number of people that can wait in the queue to connect to the server
while(true)
{
try{
waitforConnection();//this is to connect to the server
setupStreams();//setting up the input and output streams to send and receive messages
whilechatting();//this is allow to send messages using the input and output streams
}catch (Exception e)
{
showMessage("Connection has ended");//this will be displayed when the connection is ended by the server
}finally{
closecrap(); //close all the streams and sockets
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void waitforConnection() throws IOException
{
showMessage("waiting for someone to connect.....");
connection = server.accept(); //this will connect the server and the client
showMessage("Now connected to "+connection.getInetAddress().getHostName());
}
private void setupStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("Your streams are now set up......");
}
private void whilechatting() throws IOException
{
String message = "You are now connected";
sendMessage(message);
abletotype(true);
do{
try{
message = (String)input.readObject();
showMessage("\n" +message);
}catch(ClassNotFoundException e)
{
showMessage("wtf is that");
}
}while(!message.equals("CLIENT END"));
}
private void showMessage(final String a)
{
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatwindow.append(a);
}
}
);
// System.out.println(a);
}
public void closecrap()
{
showMessage("\n Connections closing .....");
abletotype(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException)
{
ioException.printStackTrace();
}
}
public void sendMessage(String message)
{
try{
output.writeObject("SERVER - "+message);//this is where you put the message in the output stream
output.flush(); //flush out the junk if there is any left
showMessage("\nSERVER -"+message); //show Message in the chat window
}catch(Exception e)
{
chatwindow.append("\n ERROR sending the message dude");
}
}
private void abletotype(final Boolean t)
{
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
usertext.setEditable(t);
}
}
);
}
}
服務器測試類:-
import javax.swing.*;
public class ServerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Server st = new Server();
st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
st.startrunning();
}
}
參考資料:- ThenewBoston.com
我將總結一些評論中所說的內容,並添加我自己的想法。
首先,該端口可能已經在使用中。 根據您所說的,它可能沒有被您的程序使用,但是其他一些程序正在使用它或保留它。 據我所知,端口 100 沒有官方用途,因此它可能只是您在機器上安裝的另一個程序。 同樣從評論看來,您已經通過交換到不同的端口來解決這個問題。
Connection.close() 應該可以正常關閉連接。 有關更多信息,請查看此鏈接: 如何以正確的方式關閉套接字?
就您最近的評論而言 - 如果程序正在運行但未顯示 UI,則您可能會遇到與您發布的問題無關的代碼的另一個問題。 我可能建議嘗試從這里開始自己調試它,因為服務器正在啟動而沒有 JVM_BIND 錯誤,看看你想出了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.