Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Read/Writer Thread problem in Socket

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Report post to moderator
Here are two threads , one for writing on a port and other for reading from the port ,




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 .

But this is not working , plz help me
--------------------

faisal
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Report post to moderator
I answered this two days ago here. Please don't waste people's time by posting the same question in multiple forums. See here for an explanation of why I'm closing this thread.
 
Get off me! Here, read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic