Why am I not receiving the message? [Sockets]
I'm just trying to do something simple like have the computer you connect to send you a message containing the date, but I don't seem to be doing it right. I'm fairly new to java so I may be just making simple mistake but here's the code I'm trying to use as of right now;
Code:
import java.io.*;
import java.net.*;
public class main {
static int loop = 1;
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws UnknownHostException, IOException {
connect();
}
public static void connect() throws UnknownHostException, IOException{
System.out.print("Specify server to connect to.\n-> ");
String ipee = input.readLine();
System.out.print("Specify port to connect to.\n-> ");
String prt = input.readLine();
int port = Integer.parseInt(prt);
Socket client = new Socket(ipee, port);
InetAddress inet = client.getInetAddress();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("Connection established to "+ inet);
System.out.println(in.readLine());
}
}
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class main {
static BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
static Date date = new Date();
static int loop = 1;
public static void main(String[] args) throws IOException {
connect();
}
public static void connect() throws IOException{
while(loop == 1){
ServerSocket server = new ServerSocket(3030);
Socket client = server.accept();
InetAddress inet = client.getInetAddress();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter msg = new PrintWriter(client.getOutputStream(), true);
msg.print("Connection established on "+ date.getDate());
server.close();
client.close();
}
}
}
Edit: I probably should have stated the problem, which is the fact that when I connect I don't actually receive the message so may be I should be sending it another way?