Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80aaee1

Browse files
socket programming
1 parent45e0a8e commit80aaee1

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagesporadic.socket_programming;
2+
3+
importjava.io.DataInputStream;
4+
importjava.io.DataOutputStream;
5+
importjava.io.IOException;
6+
importjava.io.InputStream;
7+
importjava.io.OutputStream;
8+
importjava.net.Socket;
9+
10+
/**
11+
* The following GreetingClient is a client program that connects to a server by
12+
* using a socket and sends a greeting, and then waits for a response.
13+
*/
14+
15+
publicclassGreetingClient {
16+
publicstaticvoidmain(String[]args) {
17+
StringserverName =args[0];
18+
intport =Integer.parseInt(args[1]);
19+
try {
20+
System.out.println("Connecting to " +serverName +" on port "
21+
+port);
22+
Socketclient =newSocket(serverName,port);
23+
System.out.println("Just connected to "
24+
+client.getRemoteSocketAddress());
25+
OutputStreamoutToServer =client.getOutputStream();
26+
DataOutputStreamout =newDataOutputStream(outToServer);
27+
28+
out.writeUTF("Hello from " +client.getLocalSocketAddress());
29+
InputStreaminFromServer =client.getInputStream();
30+
DataInputStreamin =newDataInputStream(inFromServer);
31+
System.out.println("Server says " +in.readUTF());
32+
client.close();
33+
}catch (IOExceptione) {
34+
e.printStackTrace();
35+
}
36+
}
37+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
packagesporadic.socket_programming;
2+
3+
importjava.io.DataInputStream;
4+
importjava.io.DataOutputStream;
5+
importjava.io.IOException;
6+
importjava.net.ServerSocket;
7+
importjava.net.Socket;
8+
importjava.net.SocketTimeoutException;
9+
10+
// TODO: this program not working yet, make it work or start from scratch to write another socket
11+
// program to better understand socket.
12+
13+
/**
14+
* Compile client and server and then start server as follows:
15+
*
16+
* $ java GreetingServer 6066 Waiting for client on port 6066... Check client
17+
* program as follows:
18+
*
19+
* $ java GreetingClient localhost 6066 Connecting to localhost on port 6066
20+
* Just connected to localhost/127.0.0.1:6066 Server says Thank you for
21+
* connecting to /127.0.0.1:6066 Goodbye!
22+
*
23+
* But it's not working as above, running from Eclipse and from terminal,
24+
* neither worked. I'll have to figure out why it didn't work and make it work
25+
* in the future./
26+
*
27+
* /** The following GreetingServer program is an example of a server
28+
* application that uses the Socket class to listen for clients on a port number
29+
* specified by a command-line argument:
30+
*/
31+
publicclassGreetingServerextendsThread {
32+
privateServerSocketserverSocket;
33+
34+
publicGreetingServer(intport)throwsIOException {
35+
serverSocket =newServerSocket(port);
36+
serverSocket.setSoTimeout(10000);
37+
}
38+
39+
publicvoidrun() {
40+
while (true) {
41+
try {
42+
System.out.println("Waiting for client on port "
43+
+serverSocket.getLocalPort() +"...");
44+
Socketserver =serverSocket.accept();
45+
System.out.println("Just connected to "
46+
+server.getRemoteSocketAddress());
47+
DataInputStreamin =newDataInputStream(
48+
server.getInputStream());
49+
System.out.println(in.readUTF());
50+
DataOutputStreamout =newDataOutputStream(
51+
server.getOutputStream());
52+
out.writeUTF("Thank you for connecting to "
53+
+server.getLocalSocketAddress() +"\nGoodbye!");
54+
server.close();
55+
}catch (SocketTimeoutExceptions) {
56+
System.out.println("Socket timed out!");
57+
break;
58+
}catch (IOExceptione) {
59+
e.printStackTrace();
60+
break;
61+
}
62+
}
63+
}
64+
65+
publicstaticvoidmain(String[]args) {
66+
intport =Integer.parseInt(args[0]);
67+
try {
68+
Threadt =newGreetingServer(port);
69+
t.start();
70+
}catch (IOExceptione) {
71+
e.printStackTrace();
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp