• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

full duplex chatty in Java (CODE PASTED)

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello sheriff,

I am pasting my code
--------------------------------------------------------------------------
this is client's part
import java.io.*;
import java.net.*;

class host2 extends Thread
{
Socket sock ; SktReader sr ; SktWriter sw ;

public void start_host2()
{
try{
sock=new Socket("localhost" , 5999);
sr= new SktReader(sock);
sw= new SktWriter(sock);

sr.start();
sw.start();


for(;
{
try {
Thread.sleep(2000);
}catch(InterruptedException e)
{
e.printStackTrace();return;
}
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
}
}

class client
{
public static void main(String a[])

{
host2 c1=new host2();
c1.start_host2();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter String");
h.sw.myresume(br.readLine().toString());// Input should go from here only
h.sw.mysuspend();


}
}
____________________________________________________________________________

This is for continuously listing from port(SktReader.java)
___________________________________________________________________________


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

class SktReader extends Thread
{
Socket socket; SktWriter sw;

public SktReader(Socket s)
{
socket = s;
}

public void run()
{
try
{
for(; // Infinite Looping
{
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str;
while ((str = rd.readLine()) != null)
{System.out.println(str);
rd.close(); System.out.println("PRINTING NULL");}
}
} catch (IOException e)
{
System.err.println(e);
}
}

}

____________________________________________________________________________
This is for writing on port(SktWriter)
____________________________________________________________________________
import java.io.*;
import java.net.*;
import java.lang.*;

// send output to socket
class SktWriter extends Thread
{
Socket socket; boolean setFlag = false ; String str; PrintWriter wr;

public SktWriter(Socket s)
{
socket = s;
setFlag=false;
}

public void run()
{
try
{
wr = new PrintWriter(socket.getOutputStream(), true);
while(true)
{
Thread.sleep(2000);
synchronized(this)
{
while(setFlag){
wr.println(str);
wait();
}
}

}

}catch (Exception e)
{}

wr.close();
}

public void mysuspend()
{
setFlag=false;
}

synchronized public void myresume(String st)
{
setFlag=true;
str=st;
try {notify();}catch(Exception e){}
}

}

____________________________________________________________________________
And this is server's code (server)
____________________________________________________________________________
import java.io.*;
import java.net.*;

class host1
{
ServerSocket sS ; Socket s ; SktReader sr; SktWriter sw;


host1(){}

public void start_host1() throws IOException
{
sS=new ServerSocket(5999);

while(true)
{
s=sS.accept();
sr=new SktReader(s);
sw=new SktWriter(s);
sw.start();
sr.start();
}

}
}



class server
{
public static void main(String a[]) throws IOException
{
host1 h=new host1();
h.start_host1();

//Control never comes here for taking inputs
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter String");
h.sw.myresume(br.readLine().toString());// Input should go from here only
h.sw.mysuspend();
}
}
___________________________________________________________________________

Now when this code runs , control never comes back to server class . I want to write on client machine from here only (server class)inspite of providing an i/p taking mechanism (BufferedReader br=new BufferedReader(new InputStreamReader(System.in))in the SktWriter class .

The thread should take mysuspend() funtion to supend writing and myresume()function to resume writing from server class .

The client machine too is using this meachanism for writing on the server machine .



Hope u got my point . (Yes bcoz is because)
[ January 16, 2006: Message edited by: faisal usmani ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Do you have a specific question? Is there some code that you can show us that's not working as you expected? Does "bcoz" mean "because?"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic