• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

I am in Critcal stage. Please help

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you checked out Sun's Tutorial? There is an example of a threaded server supporting multiple clients.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic