Server won't start up tutorial - 48 Testing server

+1 Julius Awesome · January 5, 2015
When I click it to run, nothing happens, second time I run it it says it's already in use. 

package server;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class second extends JFrame {

private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;

//constructor
public second(){
super("Instant Messenger");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
}

//method for starting up the server

public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
waitForConnection();
setupStreams();
whileChatting();

}catch(EOFException eofException){
showMessage("\n Server Ended Connection!");
}finally{
closeServer();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}

private void waitForConnection() throws IOException{
showMessage(" Waiting for someone to connect... \n");
connection = server.accept();
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("\n Streams has been setup! \n");
}

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 classNotFoundException){
showMessage("\n YOU WHAT MATE?");
}
}while(!message.equals("CLIENT - END"));
}

private void closeServer(){
showMessage("\n Closing connections... \n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}

private void sendMessage(String message){
try{
output.writeObject("SERVER - "+ message);
output.flush();
showMessage("\nSERVER- "+ message);
}catch(IOException ioException){
chatWindow.append("\n ERROR: YOU CAN'T TYPE THAT!");
}
}

private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}

);
}

private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}

);
}
}



Post a Reply

Replies

Oldest  Newest  Rating
0 Julius Awesome · January 6, 2015
Hey, I tried that did not work for me so I just posted it like this :D but well I will use pastebin next  time anyways, the thing is the program does not pop up, I have tried stuff, and the code seems to be correct, as I followed the tutorial and yea... , so do you have any idea why it won't show up?  http://pastebin.com/S8vFJD3b this is the code the second file and this is the main here: http://pastebin.com/qMzrKGgB 

Thanks.
0 Julius Awesome · January 6, 2015
The thing is it does not print any errors on the screen..., and I will post the code here than:) Here:

MAIN: 

import javax.swing.JFrame;

public class main {
public static void main(String args[]){

second sc = new second();
sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sc.startRunning();
}
}

THIS IS SECOND:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class second extends JFrame {

private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;

//constructor
public second(){
super("Instant Messenger");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
}

//method for starting up the server

public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
waitForConnection();
setupStreams();
whileChatting();

}catch(EOFException eofException){
showMessage("\n Server Ended Connection!");
}finally{
closeServer();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}

private void waitForConnection() throws IOException{
showMessage(" Waiting for someone to connect... \n");
connection = server.accept();
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("\n Streams has been setup! \n");
}

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 ClassNotFoundException){
showMessage("\n YOU WHAT MATE?");
}
}while(!message.equals("CLIENT - END"));
}

private void closeServer(){
showMessage("\n Closing connections... \n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}

private void sendMessage(String message){
try{
output.writeObject("SERVER - "+ message);
output.flush();
showMessage("\nSERVER- "+ message);
}catch(IOException ioException){
chatWindow.append("\n ERROR: YOU CAN'T TYPE THAT!");
}
}

private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}

);
}

private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}

);
}
}

Thanks for your time.
0 Julius Awesome · January 6, 2015
The issue is that it does not show the GUI, basically this had to display a chat server but it does not, but the video Bucky has made it worked for him...
+1 Pere Garau Burguera · January 7, 2015
In your main method, put 

sc.setVisible(true);
0 Julius Awesome · January 7, 2015
Thanks Pere:D, I just realized what I did wrong. Basically in the second class I forgot to add the setSize and setVisible, feel so embarrassed now :D 
+2 Pere Garau Burguera · January 7, 2015
:) You're welcome. You probably will remember now!
0 Julius Awesome · January 7, 2015
Thanks for you time, and I will now:) With your help now my messenger is working!:D Thanks again guys~!.
  • 1

Java / Android Development

103,770 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator