Meaningless Drivel is fun!*
The moose likes Sockets and Internet Protocols and the fly likes I am in Critcal stage. Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "I am in Critcal stage. Please help " Watch "I am in Critcal stage. Please help " New topic
Author

I am in Critcal stage. Please help

francis varkey
Ranch Hand

Joined: Sep 13, 2005
Posts: 169
I have wrote a single client server application using socket programming. In this program a message from server can view only one client. Now this program implement with Multiple clients. Whatis tecnic to implement this.If possible please give a simple excecutable program or edit my program with your logics.
Thanks in advance.


Server Program
*************************************
Server.java
**********************

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class Server extends JFrame implements KeyListener
{
Container con;
JPanel p;
JTextArea ta;
ServerSocket server;
Socket sock;
OutputStream os;
PrintWriter pr;
public Server()throws Exception
{
con=getContentPane();
con.setLayout(null);
p=new JPanel();
ta=new JTextArea(10,10);
p.setLayout(null);
ta.setBounds(30,20,100,100);
p.add(ta);
p.setBounds(0,0,200,200);
con.add(p);

ta.addKeyListener(this);
server=new ServerSocket(3000);
sock=server.accept();
os=sock.getOutputStream();
pr=new PrintWriter(os,true);


}

public void keyTyped(KeyEvent ke)
{
char ch=ke.getKeyChar();
pr.println(ch);

}
public void keyPressed(KeyEvent ke){ }
public void keyReleased(KeyEvent ke){ }



public static void main(String ar[])throws Exception
{
Server server=new Server();
server.setSize(300,300);
server.setVisible(true);
}

}


Client Program
*****************

Client.java
******************
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame
{
JPanel p;
JTextArea area;
Socket s;
InputStream is;
DataInputStream dis,dis1;
BufferedReader br;
String str;
Container con;
public Client()throws Exception
{
super("Client Application");
con=getContentPane();
con.setLayout(null);
p=new JPanel();
area=new JTextArea();
p.setLayout(null);
area.setBounds(20,30,100,100);
p.setBounds(0,0,200,200);
p.add(area);
con.add(p);
s=new Socket("127.0.0.1",3000);
is=s.getInputStream();
br=new BufferedReader(new InputStreamReader(is));
setBounds(400,0,300,300);
setVisible(true);
receiveData();
}

void receiveData()
{
while(true)
{
try
{
str=br.readLine();
area.append(str);
}
catch(Exception e){ System.out.println(e);}
} //while
} // receiveData

public static void main(String a[])throws Exception
{

Client client=new Client();
client.setSize(400,400);
client.setVisible(true);
}
} // class
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you checked out Sun's Tutorial? There is an example of a threaded server supporting multiple clients.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
Don't get me started about those stupid light bulbs.
 
subject: I am in Critcal stage. Please help