Results 1 to 2 of 2
- 04-25-2011, 04:23 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
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;
Java 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()); } }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?Java 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(); } } }
- 04-25-2011, 05:15 AM #2
Similar Threads
-
UDP Broadcast receiving
By Andrew121 in forum Advanced JavaReplies: 1Last Post: 11-02-2010, 01:42 AM -
java message box, how to not show multiple message box with same title or content
By oohay in forum AWT / SwingReplies: 6Last Post: 06-04-2010, 08:43 PM -
Receiving Message in J2ME
By java girl in forum CLDC and MIDPReplies: 2Last Post: 10-27-2009, 05:49 AM -
File and Message transfer over sockets!
By rameshraj in forum NetworkingReplies: 3Last Post: 05-14-2008, 08:13 PM -
Receiving UDP pockets
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks