dcsimg
MAC networking connection problem
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sean_Bartholomew
Posted On:   Wednesday, August 14, 2002 09:08 AM

Hi. Im using macos 9.2 and codewarrior 4.0 that only supports up to MRJ 1.1.4 Im ok with that and it forces me to not to write code that only java2 can understand. However ive download a few chat client/server examples and for the life of me i cannot get anything to connect. I keep getting “cannot connect to server” or equivalent in all catch phrases. Ive tried changing the code to 127.0.0.1 or my current ip address and launching it from within my WEBSTAR server. No dice. Its 3 FULL days now of frustration. Can u help me? Here is one of the scripts: // // Chat.java // // Dr. Dobb's Journal Ju   More>>

Hi.


Im using macos 9.2 and codewarrior 4.0 that only supports up to MRJ 1.1.4

Im ok with that and it forces me to not to write code that only java2 can understand.
However ive download a few chat client/server examples and for the life of me i cannot get anything to connect. I keep getting “cannot connect to server” or equivalent in all catch phrases.

Ive tried changing the code to 127.0.0.1 or my current ip address and launching it from within my WEBSTAR server.

No dice.


Its 3 FULL days now of frustration.

Can u help me?

Here is one of the scripts:




			

//
// Chat.java
//
// Dr. Dobb's Journal June 1996 (Java Q&A Column)
//
// By Cliff Berg of Digital Focus, http://www.digitalfocus.com
//
// Rewritten by Michael Puening.
// (Notice the extensive changes)
//

import java.io.*;
import java.awt.*;
import java.applet.*;
import java.net.*;

/******************************************************************
*
* ServerChat
*
* A TextArea thread that listens to the socket for incoming data.
*
******************************************************************/

class ServerChat extends TextArea implements Runnable
{
Chat chat; // Need the get at socket
public Thread thread;
public int curpos = 0;
final static int MAXLENGTH = 5000;
final static int TEXTAREA_WIDTH = 50;
final static int TEXTAREA_HEIGHT = 8;

/**********************************************************
* The ServerChat constructor.
*
* Create a TextArea, set it to be not editable, allocate a thread
* And away we go. That Chat class allows us to get to the data
* input stream.
*/

public ServerChat(Chat c)
{
super(TEXTAREA_WIDTH, TEXTAREA_HEIGHT);
chat = c;
setEditable(false);

thread = new Thread(this);
}

/*********************************************************
* ServerChat update()
*
* Called when the component is exposed, or when repaint() is called.
*/

public void update(Graphics g)
{
paint(g);
}

/********************************************************
* ServerChat readln()
*
* Read data from the socket. Blocks.
*/

public String readln(DataInputStream dis)
throws IOException
{
byte b[] = new byte[MAXLENGTH];

String s;
int nchars = dis.read(b, 0, MAXLENGTH);
if (nchars > 0) {
s = new String(b, 0, 0, nchars);
}
else {
s = "";
}

return s;
}

/***********************************************************
* ServerChat run
*
* The thread's main routine, in effect. Called by Thread.start().
*/

public void run()
{
// Read from the socket;
// When there is data, post an event to the applet

String inData = null;
for (;;) {
if (chat.connected == false)
continue;
// Wait on a socket read...
try {
inData = readln(chat.dis);
}
catch (IOException ex) {
System.out.println("IO Exception reading " +
"from server " + ex.toString());
}

// Write the data to the text area...
insertText(inData, curpos);
curpos += inData.length();
}
}
}

/*******************************************************************
*
* Chat
*
* The main applet.
*
******************************************************************/

public class Chat extends Applet
{
// applet dimensions; browser overrides
final static int DEFAULT_FRAME_WIDTH = 600;
final static int DEFAULT_FRAME_HEIGHT = 400;

final static String HOSTNAME = "127.0.0.1";
final static int PORTNUMBER = 9999;

// Socket Connection data
String hostname;
int port;
Socket socket;
public DataInputStream dis;
public DataOutputStream dos;

// Input data
TextField userName;
TextField userMsg;
ServerChat serverChat;

// flag indicating if we are connected
boolean connected = false;

/************************************************************
*
* Chat constructor.
*/

public Chat()
{
// Set layout appropriately
setLayout(new BorderLayout());

// Create input fields
userName = new TextField("Sean",12);
userMsg = new TextField("better work...", 28);

// Create output window
serverChat = new ServerChat(this);
Panel controls = new Panel();
controls.setLayout(new FlowLayout(FlowLayout.LEFT));
controls.add(new Label("Name:", Label.CENTER));
controls.add(userName);
controls.add(new Label("Message:", Label.CENTER));
controls.add(userMsg);

add("South", controls);
add("Center", serverChat);

}

/**********************************************************
*
* Chat init()
*
* Establish connection with server and start the monitor thread.
*/

public void init()
{
resize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
try {
makeConnection(HOSTNAME, PORTNUMBER);
}
catch (Exception ex) {
System.out.println("Unable to make connection to host");
serverChat.insertText("Sorry. Unable to make " +
"connection to server.
", serverChat.curpos);
connected = false;
return;
}
connected = true;
}

/***********************************************************
*
* Chat Start
*/

public void start()
{
if (! connected) {
// Hmmm... maybe user returned to page.
try {
makeConnection(HOSTNAME, PORTNUMBER);
connected = true;
getLayout().removeLayoutComponent(serverChat);
serverChat = new ServerChat(this);
add("Center", serverChat);
}
catch (Exception ex) {
connected = false;
}
}

if (! connected) {
// It's hopeless, let's give up.
return;
}
serverChat.thread.start(); // calls run()
}

/*********************************************************
*
* Chat stop
*
* Called when browser leaves this page.
*/

public void stop()
{
serverChat.thread.stop();
try {
socket.close();
}
catch (IOException ex) {
System.out.println(ex);
}
connected = false;
}

/***********************************************************
*
* Chat makeConnection
*
* Establish a connection with the chat server program.
*/

void makeConnection(String h, int p)
throws
java.net.UnknownHostException,
IOException
{
hostname = h;
port = p;

socket = new Socket(hostname, port);

// Issue confirmation to user
InetAddress rina = socket.getInetAddress();
System.out.println("Connected to " + rina.toString());
InetAddress lina = rina.getLocalHost();
System.out.println("Local host designation:" + lina.toString());

// Determine the socket's input and output stream handles
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
}

/**********************************************************
*
* Chat getMsg
*
* Build a string based on user's input.
*/

public String getMsg()
{
String s, n;

n = userName.getText();
s = userMsg.getText();

if (s.length() == 0)
return "";

if (n.length() == 0)
n = "Anonymous";

// Append a newline character
s = n + " : " + s + "
";

// Clear the message buffer
userMsg.setText("");
return s;
}

/**********************************************************
*
* Chat keyDown
*
* Handle key presses. Write data to server when return is pressed.
*/

public boolean keyDown(Event evt, int key)
{
if ( (char)key == '
') {
String s = getMsg();

// Send nothing if there's nothing
if (s.length() == 0)
return false;

if (!connected)
return false;

// Send the input to the server
try {
dos.writeBytes(s);
}
catch (IOException ex) {
System.out.println("IO Exception writing "+
"to server" + ex.toString());
}

return true;
}
return false;
}

/********************************************************
*
* Chat main()
*
* We only need this if we run standalone (not in a browser).
*/

public static void main(String args[])
{
ChatFrame f = new ChatFrame("Chat");
f.chat = new Chat();

f.add("Center", f.chat);

f.resize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
f.show();

f.chat.init();
f.chat.start();
}

/*********************************************************
* Chat update()
*
* Called when the component is exposed, or when repaint() is called.
*/

public void update(Graphics g)
{
paint(g);
}

}

/*************************************************************
*
* ChatFrame
*
* A closeable frame. Used only by Chat.main().
*
*************************************************************/

class ChatFrame extends Frame
{
Chat chat;

public ChatFrame(String s)
{
super(s);
}

public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY) {
System.exit(0);
}
return super.handleEvent(evt);
}
}

   <<Less
About | Sitemap | Contact