Sockets

J

James

I have the following code but when I try to send message beteewn client both
have to enter a string before the first string appears on the other person
screen how can I make it so that when first start the cleint and enter a
string it appears stright away not only after they enter a string?



import java.io.*;
import java.net.*;
import java.util.*;
public class Client{
private static final int PORTNUM = 44130;
public static void main(String[] args) {
Socket socket;
DataInputStream in;
PrintStream out;
String address;


try {
socket = new Socket("localhost", PORTNUM);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());

String str,inStr;
int c;
Scanner sc = new Scanner(System.in);
while (true) {
str = sc.nextLine();
out.println(str);
out.flush();
while(Integer.parseInt(in.readLine())!=0) {}
inStr = in.readLine();
System.out.println(inStr);
if(inStr.compareTo("")==0) break;

}
out.close(); in.close(); socket.close();
}
catch (IOException e) { System.err.println(e); }
}
}



import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.*;

public class ChatServer
{

private int count = 1;
public static void main(String[] arguments) {
new ChatServer();
}

public ChatServer()
{

ServerSocket sSock = null;


try {
sSock = new ServerSocket(44130);
System.out.println("ChatServer up and running ...");
}
catch (Exception e) {
System.err.println(e);
System.exit(1);
}
while ( true)
{
try {
ChatThread t = new ChatThread(sSock.accept(),sSock.accept());
t.start();
System.out.println("ChatThread " + count++ + " Started ...");
}
catch (IOException e) {
System.err.println(e);
System.exit(1);
}
}
}
class ChatThread extends Thread
{
private Socket clienta = null;
private Socket clientb = null;
private int numQuestions;
private int num = 0;


public ChatThread(Socket socka, Socket sockb) {
super("ChatThread");
clienta = socka;
clientb= sockb;
}
public void run() {

try {
InputStreamReader isra = new
InputStreamReader(clienta.getInputStream());
BufferedReader isa = new BufferedReader(isra);
PrintWriter osa = new PrintWriter(new
BufferedOutputStream(clienta.getOutputStream()),
false);

InputStreamReader isrb = new
InputStreamReader(clientb.getInputStream());
BufferedReader isb = new BufferedReader(isrb);
PrintWriter osb = new PrintWriter(new
BufferedOutputStream(clientb.getOutputStream()),
false);



while (true) {
String inLinea = isa.readLine();
osb.println(0);
osb.println(inLinea);
osb.flush();

String inLineb = isb.readLine();
osa.println(0);
osa.println(inLineb);
osa.flush();

if(inLineb.compareTo("")==0 || inLinea.compareTo("")==0)
break;
}

osa.close();
isa.close();
clienta.close();
osb.close();
isb.close();
clientb.close();
}
catch (Exception e) {
System.err.println("Error: " + e);
e.printStackTrace();
}
}

}
}
 
B

Bart Cremers

Split reading the input from the keyboard and reading the input from
the server in two different threads.
Now you're waiting for keyboard input and only if that's finished you
continue to read a line from the server.

Bart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top