[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Missing post? - see this post about spam for more information.
Messages posted by: thornt5748
Forum Index » Profile for thornt5748 » Messages posted by thornt5748
Author Message
First things, thank you for your reply, secondly how would I do this? What would the coding be?

Thanks
Hi Guys,

I'm quite new here and am new to Java Programming, but i could use some help. I have a client-serve chat program and have almost finished it to my standards however I am trying to enable the server to accept multiple clients, how would i do this? what do i have to add or change on the server to allow this?

Thanks

Server
Hi Guys, I have this "Server" application that I run and compile through that command prompt in windows. This runs in conjunction with a client version of this script and extends to a java gui application. This program takes whats written within a text area on the server and outputs it to a text area on the client program. Every time a new message is typed and sent it over writes what has already been sent. I want to do it so that when I send another line to it will displays on a new line rather than overwrite the previous. What do i need to put in and where? Thanks very much in advance


import java.awt.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class SwingChatServer extends SwingChatGUI
{
PrintWriter out;
BufferedReader in;
BufferedReader stdin;
String inputLine, outputLine;
public ButtonHandler bHandler = new ButtonHandler();

public SwingChatServer (String title)
{
super (title);
bHandler = new ButtonHandler ();
sendButton.addActionListener (bHandler);
}

private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String outputLine;
outputLine = InputArea.getText ();
System.out.println ("Server > " + outputLine);
out.println (outputLine);
}
}

public void run () throws IOException
{
ServerSocket serverSocket = null;

try
{
serverSocket = new ServerSocket (4444);
}
catch (IOException e)
{
System.err.println ("Could not listen on port: 4444.");
System.exit (1);
}

Socket clientSocket = null;
try
{
clientSocket = serverSocket.accept ();
}
catch (IOException e)
{
System.err.println ("Accept failed.");
System.exit(1);
}

out = new PrintWriter (clientSocket.getOutputStream (), true);
in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
//stdin = new BufferedReader (new InputStreamReader (System.in));

out.println ("Welcome to the Chat Server\n");

while ((inputLine = in.readLine ()) != null)
{
System.out.println ("Server < " + inputLine);
OutputArea.setText (inputLine);
}

out.close();
in.close();
clientSocket.close();
serverSocket.close();
}

public static void main(String[] args) //throws IOException
{

SwingChatServer frame = new SwingChatServer ("Chat Server Program");

frame.pack ();
frame.setVisible(true);
try
{
frame.run ();
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: 194.81.104.118.");
System.exit(1);
}

}
}
Thanks for such a speedy reply, I really appreciate your feedback. I have attempted what you said but like I mentioned I have no idea what i'm doing, so it didn't work lol. Thanks anyway though.


import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import javax.swing.*;

public class DatagramGUI extends JFrame implements ActionListener {

private JPanel jPMain;
private JLabel jLHost;
private JTextField jTFHost;
private JButton jBGet;
private JTextField jTFResult;


public static void main
{
DatagramGUI frame = new DatagramGUI();
frame.setSize(500, 300);
frame.DatagramGUI();
frame.setVisible(true);
frame.setTitle("Request date and time");
frame.setLocationRelativeTo(null);
}

private void DatagramGUI() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());

jPMain = new JPanel();
jPMain.setLayout(new GridLayout (4,1));
window.add(jPMain);

jLHost = new JLabel("Enter Host IP");
jPMain.add(jLHost);

jTFHost = new JTextField("");
jPMain.add(jTFHost);

jBGet = new JButton("Request Date and Time");
jPMain.add(jBGet);
jBGet.addActionListener(this);

jTFResult = new JTextField("");
jPMain.add(jTFResult);

}
public void actionPerformed(ActionEvent event)
{
if (args.length != 1)
{
System.out.println("Usage: java DatagramClient <hostname>");
return;
}

// get a datagram socket
DatagramSocket socket = new DatagramSocket();

// send request
byte[] buf = new byte[256];
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4455);
System.out.println ("About to request date & time");
socket.send(packet);
System.out.println ("Sent request for date and time");

// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println ("Received date & time");

// display response
String received = new String(packet.getData());
System.out.println("Remote Date and Time: " + received);

socket.close();
}

}
Hi guys,

I know i'm new here but i'm really in need of some help, I have been given a client side Java snippet of code that request the data and time from a Java server and have been tasked to combine it with a GUI interface. I have start the make the GUI but I have no idea how to incorporate the client request. I know there may be other ways of creating the GUI but it needs to be done like this, and the coding needs to be as simple as possible. I know you can't actually combine to two codes directly and have to add the request as an on-click action to a button but I have no idea how t do this as I am a complete novice.

Thanks in advance

Here's the GUI Coding

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DatagramGUI extends JFrame {

private JPanel jPMain;
private JLabel jLHost;
private JTextField jTFHost;
private JButton jBGet;
private JTextField jTFResult;


public static void main (String[] args)
{
DatagramGUI frame = new DatagramGUI();
frame.setSize(500, 300);
frame.DatagramGUI();
frame.setVisible(true);
frame.setTitle("Request date and time");
frame.setLocationRelativeTo(null);
}

private void DatagramGUI() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());


jPMain = new JPanel();
jPMain.setLayout(new GridLayout (4,1));
window.add(jPMain);

jLHost = new JLabel("Enter Host IP");
jPMain.add(jLHost);

jTFHost = new JTextField("");
jPMain.add(jTFHost);

jBGet = new JButton("Request Date and Time");
jPMain.add(jBGet);

jTFResult = new JTextField("");
jPMain.add(jTFResult);

}
}


Here's the client/server Request Coding


import java.io.*;
import java.net.*;
import java.util.*;

/*
Compile: javac DatagramClient.java
Run: java DatagramClient // DatagramServer must be running
*/

public class DatagramClient
{
public static void main(String[] args) throws IOException
{

if (args.length != 1)
{
System.out.println("Usage: java DatagramClient <hostname>");
return;
}

// get a datagram socket
DatagramSocket socket = new DatagramSocket();

// send request
byte[] buf = new byte[256];
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4455);
System.out.println ("About to request date & time");
socket.send(packet);
System.out.println ("Sent request for date and time");

// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println ("Received date & time");

// display response
String received = new String(packet.getData());
System.out.println("Remote Date and Time: " + received);

socket.close();
}
}
 
Forum Index » Profile for thornt5748 » Messages posted by thornt5748
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2012 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle