Hi, i created a program but i need some help. I made it so the user must enter the ip address and then click the button CONNECT. But in java it outputs the same thing. Is there any way i can get rid of it and just start the program with the connect button only. I will post the code when i go to school tommorow. I will grab it off my computer at school.

Hi Joker21,

If you want to talk to us then you approach the daniweb, same way your java program need a valid and available IP-Address. Yes, It is also possible that you might have no permission or a firewall reject the connection request.

Ok, the connection goes through. I got the connection to work, now i want to know this: i can send a message the other computer receives the message. but that computer can not return a message to me. How do you do this ? here is the code that i am using

//The client code Client.java:


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

public class TestClient {

String ip;
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;

// Constructor connection receiving a socket number
TestClient(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());

/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the ip address to connect to:");
ip = scan.next();
// send the string to the server
System.out.println("Client sending \"" + ip + "\" to serveur");
try {
Soutput.writeObject(ip);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}

try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}

public static void main(String[] arg) {
new TestClient(1500);
}
}

//The server code Server.java:

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

/**
* This is to help people to write Client server application
* I tried to make it as simple as possible... the client connect to the server
* the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class TestServer {

// the socket used by the server
private ServerSocket serverSocket;
// server constructor
TestServer(int port) {

/* create socket server and wait for connection requests */
try
{
serverSocket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());

while(true)
{
Socket socket = serverSocket.accept(); // accept connection
System.out.println("New client asked for a connection");
TcpThread t = new TcpThread(socket); // make a thread of it
System.out.println("Starting a thread for a new Client");
t.start();
}
}
catch (IOException e) {
System.out.println("Exception on new ServerSocket: " + e);
}
}

// you must "run" server to have the server run as a console application
public static void main(String[] arg) {
// start server on port 1500
new TestServer(1500);
}

/** One instance of this thread will run for each client */
class TcpThread extends Thread {
// the socket where to listen/talk
Socket socket;
ObjectInputStream Sinput;
ObjectOutputStream Soutput;

TcpThread(Socket socket) {
this.socket = socket;
}
public void run() {
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output Streams");
try
{
// create output first
Soutput = new ObjectOutputStream(socket.getOutputStream());
Soutput.flush();
Sinput = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
System.out.println("Thread waiting for a String from the Client");
// read a String (which is an object)
try {
String str = (String) Sinput.readObject();
str = str.toUpperCase();
Soutput.writeObject(str);
Soutput.flush();
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
// will surely not happen with a String
catch (ClassNotFoundException o) {
}
finally {
try {
Soutput.close();
Sinput.close();
}
catch (Exception e) {
}
}
}
}
}


I am using this code from another website but can u teach me how to make it so that the user on the computer who received the message can send a message back?

adatapost> Please place your code in BB code tag. I have tested your code. Your code is ok and it produce the same result you want.

No, ok here is what i want . Computer 1 can send a message to Computer 2 But Computer 2 cannot send a message back. Computer 2 can only see the message that Computer 1 sent them

i do not know if i understand it properly but,

both computers have to run server and client application in order to communicate equally.

also if you implement a multithread server, you will be able to handle multiple clients. ie. computer1 acts as a server, computer2 and other computers that are running client application may interact with server.

well what im trying to get at is to make a mini-version of msn. Do you know how i could do that? do i have to use 2 or 3 comptuers? so from this i assume all i have to do is have 1 computer be the server, and then computer 2 and 3 can talk with each other?

This article has been dead for over six months. Start a new discussion instead.