• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sockets and Threads

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
if U have that much of patience tell me why sending to Client is not achieved while receiving is possible from client.
Ofcoarse that too works only if I commented the sendall class and its thread. The sendall thread broadcasts to everyclients that is being stored in the Vector.
Help me if U could!!! plz............
Below is the Server side program:
import java.net.*;
import java.io.*;
import java.util.*;
public class Forsc1
{
static Vector listof=new Vector();
public static void main(String args[])
{
Socket Client;
try
{
ServerSocket ss=new ServerSocket(5555);
while(true)
{
Client=ss.accept();
Receive r=new Receive(Client);
System.out.println("connected: "+Client.getInetAddress().getHostName());
r.start();
listof.add(Client);
Sendall toall=new Sendall();
toall.start();
}
}
catch (Exception e)
{}
}
}
class Receive extends Thread
{
Socket client;
Vector v=new Vector();
Vector history=new Vector();
ObjectInputStream in;
Object o;
public Receive(Socket c)
{
client=c;
try
{
in=new ObjectInputStream(client.getInputStream());
}
catch(Exception e)
{}
}
public void run()
{
int h,q=0;
try
{
synchronized(o)
{
q++;
o=in.readObject();
v=(Vector) o;
for(h=0;h<v.size();h++)>
{
System.out.println("received :"+v.elementAt(h));
}
sleep(100);
}
}
catch(Exception e)
{}
}
}
class Sendall extends Thread
{
Vector all;
Socket s;
Object came;
ObjectOutputStream op;
Forsc con=new Forsc();
Receive rec;
public Sendall()
{
all=con.listof;
came=rec.o;
System.out.println("no.of clients ====="+all.size());
}
public void run()
{
for(int k=0;k<all.size();k++)>
{
try
{
s=(Socket) all.elementAt(k);
op=new ObjectOutputStream(s.getOutputStream());
System.out.println("sending to "+s);
op.writeObject(came);
}
catch(Exception e)
{}
}
}
}

______________________________________________________________
Below is the Clientside program
import java.io.*;
import java.net.*;
import java.util.*;
public class Forc
{
static Socket socks;
public static void main(String args[])
{
try
{
socks=new Socket("localhost",5555);
}
catch(Exception e)
{}
Send s=new Send(socks);
s.start();
Receive r=new Receive(socks);
r.start();
}
}
class Send extends Thread
{
Socket socks;
ObjectOutputStream o;
Vector v=new Vector();
public Send(Socket s)
{
socks=s;
try
{
o=new ObjectOutputStream(socks.getOutputStream());
}
catch(IOException e)
{}
for(int i=1;i<=100;i++)
{
v.addElement(new Integer(i));
}
}
public void run()
{
try
{
synchronized(o)
{
o.writeObject(v);
try
{
this.sleep(1000);
}
catch(Exception e)
{}
System.out.println("sended "+v.elementAt(y));
}
}
catch(IOException e)
{}
}
}
class Receive extends Thread
{
ObjectInputStream ois;
Vector g=new Vector();
Socket socks;
Object oops;
public Receive(Socket s)
{
socks=s;
try
{
ois=new ObjectInputStream(socks.getInputStream());
}
catch(IOException e)
{}
}
public void run()
{
try
{
oops=ois.readObject();
if(oops instanceof Vector)
{
g=(Vector) oops;
}
for(int i=0;i<g.size();i++)>
{
System.out.println("received "+g.elementAt(i));
}
}
catch(Exception e)
{}
}
}

Thanking U



 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not post the same message in multiple forums! Post it to the forum that you feel is best for the question. If a moderator feels that it will better be answered in another forum, we will move it there. I'm closing all these except for the one in Sockets forum.
Carl Trusiak - Sheriff
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic