View Single Post
Old 11-22-2004, 12:11 AM   #1 (permalink)
Crow T. Robot
Registered User
 
Join Date: Nov 2004
Posts: 14
Crow T. Robot is on a distinguished road
Datagrams, multi threading, popping and pushing

Hi guys. Though I'm not all that good at Java programming, don't let my thread here fool you. It's just that it's 3 AM, my programs are due tomorrow night, and I don't have time to explain everything I know and what it is I need explained for my programs.

So, I have a few programs I need help with. I'll just retype some of the directions for making them, and display what I have so far.

For this program, it's one file of a two parter. The first must ask the user for 10 messages, and send each message as a datagram to the server.

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

public class Server extends JFrame
{
	private JTextArea displayArea;
	private DatagramSocket socket;
	
	//set up GUI and DatagramSocket
	public Server()
	{
		super("server");
		
		displayArea=new JTextArea();
		getContentPane().add(new JScrollPane(displayArea),
		BorderLayout.CENTER);
		setSize(400,300);
		setVisible(true);
		
		//create DatagramSocket for sending and receiving packets
		try
		{
			socket=new DatagramSocket(5000);
		}
		
		//process problems creating DatagramSocket
		catch(SocketException socketException)
		{socketException.printStackTrace();
		System.exit(1);
		}
		
	} //end Server constructor
	
	//wait for packets to arrive, display data and echo packet to client
	private void waitForPackets()
	{
		while (true)
		
		//loop forever
		{
			//receive packet, display contents, return copy to client
			try
			{
				//set up packet
				byte data[]=new byte[100];
				DatagramPacket receivePacket=
				new DatagramPacket(data, data.length);
				socket.receive(receivePacket); //wait for packet
				//display information received from packet
				displayMessage("/nPacket received:"
-----------------------

It's been a while since I've even dealt with this one, so I'm not sure how complete it is. The second part must accept datagrams from the client, handle each message as a thread, append the message in the datagram to a file, and use my name as part of the file name. Here's what little I have of that, though I have some good info to help finish it.
Code:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame
{
	private JTextField enterField;
	private DatagramSocket socket;
	
	//set up GUI and DatagramSocket
	public Client()
	{
		super("Client");
		
		Container container=getContentPane();
		
		enterField=new JTextField("Type message here");
		enterField.addActionListener(
		new ActionListener() {
			public void actionPerformed(ActionEvent event)
			{
				//create and send packet
				try {
					displayArea.append("/nSending packet containing: " +
					event.getActionCommand() + "/n");
----------------------

I'll put my other programs up in additional posts. And please, try and guide me in the right direction, help me with syntax, errors, etc. I'm not trying to cheat, and will need to be able to understand this stuff. Chunks of code, definitions, etc. are all strongly appreciated as well, though keep in mind I may have to adapt them.

Last edited by Belisarius; 11-22-2004 at 05:20 AM.
Crow T. Robot is offline   Reply With Quote