Skip to content

Commit 5349fbf

Browse files
author
bob
committed
Commit
1 parent 662a934 commit 5349fbf

26 files changed

+533
-0
lines changed

OnMyOwn/TCPAndFun/Clienting.class

848 Bytes
Binary file not shown.

OnMyOwn/TCPAndFun/Clienting.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
3+
For this design, I will be having the client
4+
5+
sending the stream of string.
6+
7+
*/
8+
import java.util.*;
9+
import java.io.*;
10+
import java.net.*;
11+
12+
class Clienting{
13+
14+
public static void main(String args[])throws Exception{
15+
16+
InetAddress local = InetAddress.getLocalHost();
17+
18+
Socket s = new Socket(local, 7777);
19+
20+
DataOutputStream out = new DataOutputStream(s.getOutputStream());
21+
22+
//This thing sends the stream of String.
23+
out.writeUTF("Hello Server.");
24+
25+
DataInputStream in = new DataInputStream(s.getInputStream());
26+
27+
s.close();
28+
29+
}
30+
31+
}

OnMyOwn/TCPAndFun/FunClient.class

998 Bytes
Binary file not shown.

OnMyOwn/TCPAndFun/FunClient.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
3+
yeah, I don't know, I really don't
4+
5+
*/
6+
import java.util.*;
7+
import java.io.*;
8+
import java.net.*;
9+
10+
class FunClient{
11+
12+
public static void main(String args[])throws Exception
13+
{
14+
15+
String output;
16+
17+
InetAddress local = InetAddress.getLocalHost();
18+
Socket link = new Socket(local, 1234);
19+
System.out.println("Connection Successful.");
20+
21+
22+
Scanner reader = new Scanner(link.getInputStream());
23+
PrintWriter writer = new PrintWriter(link.getOutputStream());
24+
25+
String str = reader.next();
26+
System.out.println(str);
27+
28+
/*
29+
while(true){
30+
31+
output = writer.nextLine();
32+
33+
if(output.equals("close")){
34+
break;
35+
}
36+
37+
System.out.println(output);
38+
39+
}
40+
41+
42+
*/
43+
link.close();
44+
}
45+
}

OnMyOwn/TCPAndFun/FunServer.class

1.04 KB
Binary file not shown.

OnMyOwn/TCPAndFun/FunServer.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
3+
Server closing the connection
4+
5+
before any data was able to be sent.
6+
7+
*/
8+
import java.util.*;
9+
import java.io.*;
10+
import java.net.*;
11+
12+
class FunServer{
13+
14+
public static void main(String args[])throws Exception
15+
{
16+
17+
ServerSocket ss = new ServerSocket(1234);
18+
Socket link = ss.accept();
19+
System.out.println("Connection has been established");
20+
21+
//Input stream
22+
Scanner reader = new Scanner(link.getInputStream());
23+
//Output stream
24+
PrintWriter writer = new PrintWriter(link.getOutputStream());
25+
Scanner keyboard = new Scanner(System.in);
26+
27+
String str = keyboard.nextLine();
28+
29+
//writer.write(str);
30+
writer.println(str);
31+
32+
// Immediately send all data in buffer
33+
writer.flush();
34+
35+
// Tell the thread to sleep for a second
36+
// Thread.sleep(1000);
37+
/*
38+
while(true){
39+
40+
System.out.println("Please, enter your input.");
41+
send = keyboard.nextLine();
42+
43+
if(send.equalsIgnoreCase("stop")){
44+
break;
45+
}
46+
wrter.println(send);
47+
}
48+
*/
49+
link.close();
50+
51+
}
52+
}
1.62 KB
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// File Name GreetingClient.java
2+
import java.net.*;
3+
import java.io.*;
4+
5+
public class GreetingClient {
6+
7+
public static void main(String [] args) {
8+
String serverName = args[0];
9+
int port = Integer.parseInt(args[1]);
10+
try {
11+
System.out.println("Connecting to " + serverName + " on port " + port);
12+
Socket client = new Socket(serverName, port);
13+
14+
System.out.println("Just connected to " + client.getRemoteSocketAddress());
15+
OutputStream outToServer = client.getOutputStream();
16+
DataOutputStream out = new DataOutputStream(outToServer);
17+
18+
out.writeUTF("Hello from " + client.getLocalSocketAddress());
19+
InputStream inFromServer = client.getInputStream();
20+
DataInputStream in = new DataInputStream(inFromServer);
21+
22+
System.out.println("Server says " + in.readUTF());
23+
client.close();
24+
}catch(IOException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
}
2.04 KB
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// File Name GreetingServer.java
2+
import java.net.*;
3+
import java.io.*;
4+
5+
public class GreetingServer extends Thread {
6+
private ServerSocket serverSocket;
7+
8+
public GreetingServer(int port) throws IOException {
9+
serverSocket = new ServerSocket(port);
10+
serverSocket.setSoTimeout(10000);
11+
}
12+
13+
public void run() {
14+
while(true) {
15+
try {
16+
System.out.println("Waiting for client on port " +
17+
serverSocket.getLocalPort() + "...");
18+
Socket server = serverSocket.accept();
19+
20+
System.out.println("Just connected to " + server.getRemoteSocketAddress());
21+
DataInputStream in = new DataInputStream(server.getInputStream());
22+
23+
System.out.println(in.readUTF());
24+
DataOutputStream out = new DataOutputStream(server.getOutputStream());
25+
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
26+
+ "\nGoodbye!");
27+
server.close();
28+
29+
}catch(SocketTimeoutException s) {
30+
System.out.println("Socket timed out!");
31+
break;
32+
}catch(IOException e) {
33+
e.printStackTrace();
34+
break;
35+
}
36+
}
37+
}
38+
39+
public static void main(String [] args) {
40+
int port = Integer.parseInt(args[0]);
41+
try {
42+
Thread t = new GreetingServer(port);
43+
t.start();
44+
}catch(IOException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)