wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes full duplex chatty in Java (CODE PASTED) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "full duplex chatty in Java (CODE PASTED)" Watch "full duplex chatty in Java (CODE PASTED)" New topic
Author

full duplex chatty in Java (CODE PASTED)

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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?"


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: full duplex chatty in Java (CODE PASTED)
 
Similar Threads
Help with this "ready-bake code", implementing Runnable
I am in Critcal stage. Please help
Read/Writer Thread problem in Socket
Not Sure
plz help me (Socket problem)