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();
}
}
}
}
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();
}
}
}
}