-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Network Programming with Java Sockets #9
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.Socket; | ||
import java.util.Scanner; | ||
|
||
public class Client { | ||
public static void main(String[] args) { | ||
try(Socket socket = new Socket("localhost",5000)){ | ||
BufferedReader iStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
PrintWriter oStream = new PrintWriter(socket.getOutputStream(),true); | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
while(true){ | ||
System.out.print("input> "); | ||
String message = scanner.nextLine(); | ||
|
||
oStream.println(message); | ||
if(message.equalsIgnoreCase("exit")){ | ||
String response = iStream.readLine(); | ||
System.out.println(response); | ||
break; | ||
} | ||
} | ||
|
||
}catch (Exception e){ | ||
System.out.println("error: "+e.getMessage()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.io.*; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
public class Server { | ||
public static void main(String[] args) { | ||
try(ServerSocket serverSocket = new ServerSocket(5000)){ | ||
System.out.println("listening to port:5000"); | ||
Socket clientSocket = serverSocket.accept(); | ||
System.out.println(clientSocket+" connected."); | ||
BufferedReader iStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
PrintWriter oStream = new PrintWriter(clientSocket.getOutputStream(),true); | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. close stream |
||
|
||
while (true){ | ||
String message = iStream.readLine(); | ||
System.out.println("client: "+message); | ||
if(message.equalsIgnoreCase("exit")){ | ||
oStream.println("connection closed."); | ||
System.out.println("connection closed."); | ||
break; | ||
} | ||
} | ||
|
||
} catch (Exception e){ | ||
System.out.println("error: "+e.getMessage()); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preffer use logger