Hi i've been batteling for hours and can't seem to find the problem. When my server runs and I press the connect button the gui freezes until the client gui is terminated.. only then the gui becomes active again and displays the messages.
Here is my server code:
Here is protocol class which the server works from: - import java.net.*;
-
import java.io.*;
-
-
public class protocol {
-
private static final int WAITING = 0;
-
private static final int SENTKNOCKKNOCK = 1;
-
private static final int SENTCLUE = 2;
-
private static final int ANOTHER = 3;
-
-
private static final int NUMJOKES = 5;
-
-
private int state = WAITING;
-
private int currentJoke = 0;
-
-
private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
-
private String[] answers = { "Turnip the heat, it's cold in here!",
-
"I didn't know you could yodel!",
-
"Bless you!",
-
"Is there an owl in here?",
-
"Is there an echo in here?" };
-
-
public String processInput(String theInput) {
-
String theOutput = null;
-
-
if (state == WAITING) {
-
theOutput = "Knock! Knock!";
-
state = SENTKNOCKKNOCK;
-
} else if (state == SENTKNOCKKNOCK) {
-
if (theInput.equalsIgnoreCase("Who's there?")) {
-
theOutput = clues[currentJoke];
-
state = SENTCLUE;
-
} else {
-
theOutput = "You're supposed to say \"Who's there?\"! " +
-
"Try again. Knock! Knock!";
-
}
-
} else if (state == SENTCLUE) {
-
if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
-
theOutput = answers[currentJoke] + " Want another? (y/n)";
-
state = ANOTHER;
-
} else {
-
theOutput = "You're supposed to say \"" +
-
clues[currentJoke] +
-
" who?\"" +
-
"! Try again. Knock! Knock!";
-
state = SENTKNOCKKNOCK;
-
}
-
} else if (state == ANOTHER) {
-
if (theInput.equalsIgnoreCase("y")) {
-
theOutput = "Knock! Knock!";
-
if (currentJoke == (NUMJOKES - 1))
-
currentJoke = 0;
-
else
-
currentJoke++;
-
state = SENTKNOCKKNOCK;
-
} else {
-
theOutput = "Bye.";
-
state = WAITING;
-
}
-
}
-
return theOutput;
-
}
-
}
And here is the client code: - import java.io.*;
-
import java.net.*;
-
-
public class ClientForm extends javax.swing.JFrame {
-
private Socket kkSocket = new Socket();
-
private PrintWriter out = null;
-
private BufferedReader in = null;
-
/** Creates new form NewJFrame */
-
public ClientForm() {
-
initComponents();
-
}
-
-
public void send()
-
{
-
if (kkSocket.isConnected() != true)
-
statuslbl.setText("Please Connect First");
-
else
-
try
-
{
-
String client,server;
-
-
client = clientInput.getText();
-
out.println(client);
-
server = (String)in.readLine();
-
-
-
if (server.equals("Bye."))
-
{
-
in.close();
-
out.close();
-
kkSocket.close();
-
}
-
-
jTextArea1.setText(jTextArea1.getText() + "User : " + client + "\n");
-
jTextArea1.setText(jTextArea1.getText() + "Server : " + server + "\n");
-
}
-
catch (IOException e)
-
{
-
}
-
-
-
clientInput.setText("");
-
-
-
-
}
-
private void connectbtnActionPerformed(java.awt.event.ActionEvent evt) {
-
-
try
-
{
-
if (kkSocket.isConnected() == true)
-
kkSocket.close();
-
-
kkSocket = new Socket(ipText.getText(),Integer.parseInt(portText.getText()));
-
-
-
-
out = new PrintWriter(kkSocket.getOutputStream(), true);
-
in = new BufferedReader(new InputStreamReader( kkSocket.getInputStream()));
-
-
jTextArea1.setText(jTextArea1.getText() + "Server : " + (String)in.readLine() + "\n");
-
}
-
catch (Exception e)
-
{
-
statuslbl.setText("Can't Connect To Host");
-
}
-
-
if (kkSocket.isConnected() == true)
-
statuslbl.setText("Connected");
-
}
-
-
private void clientInputActionPerformed(java.awt.event.ActionEvent evt) {
-
send();
-
}
-
-
private void sendbtnActionPerformed(java.awt.event.ActionEvent evt) {
-
send();
-
-
-
}
-
-
-
-
/**
-
* @param args the command line arguments
-
*/
-
public static void main(String args[]) {
-
java.awt.EventQueue.invokeLater(new Runnable() {
-
public void run() {
-
new ClientForm().setVisible(true);
-
}
-
});
-
-
}
10 4251 JosAH 11,448
Recognized Expert MVP @Elaine121
I didn't read all your code but given your description I conclude that you do all your processing in the EDT thread (Event Dispatch Thread). You are keeping that thread busy with your accept() and read() calls; they are blocking methods, i.e. they don't return until they have done their job. The EDT doesn't have any time anymore to do its job: painting components and listening for events.
Do all of your processing in another thread so the EDT can do its job while you do your job.
kind regards,
Jos
i'm not that familiar with threads. What's the best way to do that?
JosAH 11,448
Recognized Expert MVP @Elaine121
Create an implementation of the Runnable interface that handles all the socket communication; feed it to a new Thread and start the thread. That''s basically it.
kind regards,
Jos
That sounds easy to do and I tried looking it up and I'm sorry but I have no idea what to do. Im new at this so everything is a bit complicated for me
ok I don't know if I'm on the right track with this.. in my server class I created a new thread. But now when i run the server class and press the connect button it just gives me an IOexception. - public static void main(String args[]) {
-
//java.awt.EventQueue.invokeLater(new Runnable() {
-
-
SwingUtilities.invokeLater(new Runnable() {
-
public void run() {
-
new serverForm().setVisible(true);
-
Thread newThread = new Thread(new serverForm());
-
newThread.start();
-
}
-
});
}
JosAH 11,448
Recognized Expert MVP @Elaine121
Of course the Exception stack trace told you exactly where the exception happened; reading such stack traces actually helps you finding the bug ...
kind regards,
Jos
when the port number is already in the textbox then the server runs fine.. but when i enter a number myself thats when the exception occurs. - server = new ServerSocket( Integer.parseInt(jTextField1.getText())); // this gives exception
-
server = new ServerSocket( 63100); // this runs fine
Am i reading the number wrong?
JosAH 11,448
Recognized Expert MVP @Elaine121
If that code runs before you could enter a number in the text field there won't be a number yet and the parseInt() method will throw another Exception. Otherwise (if the field contains a valid number) all will be fine.
kind regards,
Jos
So then where must i put that code? i tried a couple of places but its still the same
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Lorenzo |
last post by:
hi you all,
first of all apologies for having cross-posted this message but really
i did not know where to post it. please let me know what ng you
consider the most suitable for the described issue.
i wrote a program made by a client and a server that communicate via
RMI
premise 1) everything has been developed and executed on WinXP
Professional and the following jre
|
by: Rowland |
last post by:
Hi,
I know this question has prob. been asked a million times, but I couldn't
find it in the FAQ, so here goes :
I'm trying to write a Java applet to call a dll that resides on the web
server (running IIS 6). I've written a little test applet that should call a
helloWorld function in the dll, but when I use System.loadLibrary, it gives
me this security warning :
|
by: Krzysztof Pa¼ |
last post by:
Hi,
I want to make simple client in phyton, which would be able to communicate
with Java server using SSL sockets.
There is the Java clients, which is doing this - so I'm pretty sure, that
Java server works OK.
I've heard, that P2.3 has SSL support included in himself and also, I was
trying P2.2 with pyOpenSSL wrappers and extensions, but unsuccesfuly...
So, could you give me a few lines of python code which makes such things:
|
by: Krzysztof Paz |
last post by:
Hi,
There is a Java (SUN 1.4) server which using Object Input/Output Streams at
SSL/Socket to communicate with Java clients.
Now there is a request for making C# Client for this server also.
SSL layer could be done with Org.Mentalis.Security.dll from seclib-1.0
Mentalis package - its fine...
But, threre is a problem:
How to properly communicate from C# language to Java.ObjectInputStream -
cause I've tried many times, and always get the...
|
by: yaron |
last post by:
Hi,
I have a problem when sending data over TCP socket from c# client to java
server.
the connection established ok, but i can't send data from c# client to java
server.
it's work ok with TcpClient, NetworkStream and StreamWriter classes.
but with low level socket it doesn't work (When using the Socket class Send
method).
| |
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview with answers
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of
attending interviews. If you own a company best way to judge if the
candidate is worth of it.
http://www.questpond.com/InterviewRatingSheet.zip
|
by: oll3i |
last post by:
package library.common;
import java.sql.ResultSet;
public interface LibraryInterface {
public ResultSet getBookByAuthor(String author);
public ResultSet getBookByName(String name);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |