• Post Reply 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

plz help me (Socket problem)

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yeah. start_host1() contains an infinite loop (while (true) ...) so the call to start_host1() never returns. You need to run start_host1(), itself, in its own thread.

Now, note that I haven't looked at all of the code, just the little bit you're having a problem with. I can't tell you whether what you're doing is going to work or not, otherwise.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic