wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Socket problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket problem" Watch "Socket problem" New topic
Author

Socket problem

nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
Hi!
I post a sample code to give you the hole idea for my problem.
The truth is that this code is really simple, but the problem?


===============================================================
This is My simply server...
===============================================================
import java.io.*;
import java.net.*;
public class MyServer {

public static void main(String[] args) throws Exception {
ServerSocket server=null;

server=new ServerSocket(4664);

Socket client=null;


client=server.accept();


PrintWriter pw=new PrintWriter(client.getOutputStream(),true);
// BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
pw.println("nikos server");
pw.println("tralala");
pw.println("ade allo ena test");
pw.println("ade allo ena");
pw.close();
// br.close();
client.close();
server.close();
}

}
===========================================================
This is my simple client...
===========================================================

import java.io.*;
import java.net.*;

public class MyClient {

public static void main(String[] args) throws Exception {
Socket cs=null;
// PrintWriter out=null;
BufferedReader br=null;
InetAddress ad=InetAddress.getLocalHost();
cs=new Socket("195.170.15.185",4664);
// out=new PrintWriter(cs.getOutputStream(),true);
br=new BufferedReader(new InputStreamReader(cs.getInputStream()));

// BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String sn;
// sn=stdin.readLine();

while((sn=br.readLine())!=null)

System.out.println(sn);
// out.println("niooo");

// out.close();
br.close();
// stdin.close();
// cs.close();

}

}
=====================================================
THIS IS MY PROBLEM
=====================================================

What happens and the some data haven't been transmited?
And if the data does, why i can't see them?
This is a syncronism problem?
How can i handle them...


My problem trully is on a more difficult software module, but i have the same problem , when i'am trying to develop a specific Protocol that interacts with clients...

So , If i am true , i will found what I am missing...

==========================================================
Here are THANKSES...
==========================================================

Thanks for your time!

Farewell!!!
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Nikos,

The server socket code is not bound to any IP address. I am not sure what is the default bind, but since you are connecting to IP in the client, use the same IP address for the server socket.

I changed your code to:
Client - cs = new Socket(InetAddress.getLocalHost(), 4664);
Server - server = new ServerSocket(4664, -1, InetAddress.getLocalHost());

It works now.

Does anyone knows the defauld bind pont for ServerSocket(port) constructor?

Thank you.
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
Hi my friend!
Thanks for your posting...
I make a mistake! The ip i had post on my sample code is wrong of course.
If i use your code of course it would be work, or if i make the connection via the Loop-Back IP (127.0.0.1).

My problem is the serial transmition of the messages between Client and Server.
I mean:
The server sends a message...
The client get the message...
The client sends a message...
The server gets the message...
etc.
But In what kind of mode should The server or The Client has to be, without having the "SERVER WAITS For a message" problem...?
In this example i posted the server sends and wait...
Meanwhile the Client waits until the message comes from server and then sends His message...
This is my problem.At java.sun.com tutorial has a simple example
Check it out: Knock Knock Server and Client...
=========================================================================
import java.io.*;
import java.net.*;

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

Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}

out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}

-----------------------------------------------------------------------
import java.net.*;
import java.io.*;

public class KnockKnockProtocol {
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;
}
}
------------------------------------------------------------
import java.net.*;
import java.io.*;

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

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();

outputLine = kkp.processInput(null);
out.println(outputLine);

while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
=====================================================================
This example uses it's own Protocol that handles the messages. I'm trying something similar , but i fail...

Anyway, If you have time please give me what it's going wrong!

Thanks again for your time!
Farewell!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The basics are:
The host that is writing first, after having done so it must block on reading. The other host is expected to block on reading first, and after receiving a message form the net it must to write. The protocol object just dictates what to response according to the previous reception.


SCJP2. Please Indent your code using UBB Code
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
Hi Jose!
Thank you for your posting!

In my first example, when I am trying to have a keybort input...
(You can see it with // code-off marks)
But the prompt doesn't leave me to input strings.
Is the fault of the connection?
The problem is that I use the same philosophy of Java's tutorial, that i posted (the second example).
Maybe i have something wrong, or something on wrong timing...
Is all about timing?

Anyway, I'll check it on another view, but if you have something on mind please post it to me!


Thanks!
Randy Liu
Greenhorn

Joined: May 24, 2004
Posts: 1
nikos,
I might be the least qualified to answer this, but I'm right about having almost exactly the same problem as yours, thanx to your mentioning of Sun's Java Tutorial, I somehow managed to solve my problem.

I figure that you are trying to make the server says something to the client, and the client host's user key-in something and throws back to server. If that's the case, you code could be modified as below:

-------------------------------
MyServer
-------------------------------
import java.io.*;
import java.net.*;
public class MyServer {

public static void main(String[] args) throws Exception {
ServerSocket server=null;

server=new ServerSocket(4664);

Socket client=null;


client=server.accept();


PrintWriter pw=new PrintWriter(client.getOutputStream(),true);
BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
pw.println("nikos server");
System.out.println(br.readLine());
pw.println("tralala");
System.out.println(br.readLine());
pw.println("ade allo ena test");
System.out.println(br.readLine());
pw.println("ade allo ena");
System.out.println(br.readLine());
pw.close();
br.close();
client.close();
server.close();
}

}

-------------------------------
MyClient
-------------------------------
import java.io.*;
import java.net.*;

public class MyClient {

public static void main(String[] args) throws Exception {
Socket cs=null;
PrintWriter out=null;
BufferedReader br=null;
InetAddress ad=InetAddress.getLocalHost();
cs=new Socket("192.168.0.3",4664);
out=new PrintWriter(cs.getOutputStream(),true);
br=new BufferedReader(new InputStreamReader(cs.getInputStream()));

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String sn;
while((sn=br.readLine())!=null){
System.out.println(sn); //<-The server says what?
sn=stdin.readLine(); //<-The client user replies.
out.println("echo: " + sn); //<-The client host sends user input to server
}
out.println("niooo");

out.close();
br.close();
stdin.close();
cs.close();
}
}

It ain't pretty, but functional.

I haven't figure out what the KnockKnockProtocal means in Sun's Tutorial.


A new, little Duke servant
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
Hi there!

Hmmm, your code has clean up my problem, i was confused.

You said that you didn't understand what the KnockKnockProtocol does right?
Then, I believe that when you want to develop a socket-type software module, then you will need a protocol for safe connection and data-transmission!
Check it out better!I believe it's a good lesson (for me, anyway), but your code give me the hint i wanted!
Try look the protocol carefully, i will do the same, and if you or i found something, will post the results.
I will check it out on my project, thanks my friend!



==================================
 
wood burning stoves
 
subject: Socket problem
 
Similar Threads
client-serer...plz help
i want create a simple chat with private rooms
FTP Socket Programming
Server and Client communication probs
Send a String to Server and server send it somewhere on network