Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am in Critcal stage. Please help

 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked out Sun's Tutorial? There is an example of a threaded server supporting multiple clients.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic