• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

I am in Critcal stage. Please help

 
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
 
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.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the 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
reply
    Bookmark Topic Watch Topic
  • New Topic