[java] serverclient
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import java.net.*;
- import java.util.Scanner;
- import java.io.*;
- public class server {
- public static void main(String[] args) throws Exception {
- try {
- ServerSocket s = new ServerSocket(7777);
- System.out.println("Server is up... Waiting for the client to connect");
- Socket ss = s.accept();
- DataOutputStream sout = new DataOutputStream(ss.getOutputStream());
- DataInputStream din = new DataInputStream(ss.getInputStream());
- String str="",str2="";
- str=din.readUTF();
- System.out.println("client says:" + str);
- sout.writeUTF("u are welcome");
- sout.flush();
- str2=din.readUTF();
- System.out.println("client says: " + str2);
- if(str2=="bye") {
- System.out.println("server goes down");
- s.close();
- }
- }catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- import java.net.*;
- import java.io.*;
- import java.util.Scanner;
- public class client {
- public static void main(String[] args) {
- try {
- Scanner key = new Scanner(System.in);
- Socket ss = new Socket("localhost", 7777);
- DataOutputStream ssout = new DataOutputStream(ss.getOutputStream());
- DataInputStream ssout1 = new DataInputStream(ss.getInputStream());
- ssout.writeUTF("Hello from client. Thank you for connecting me to the server");
- ssout.flush();
- String str="";
- str = ssout1.readUTF();
- System.out.println("server says:" + str);
- String x = key.nextLine();
- ssout.writeUTF(x);
- ssout.flush();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Editor
You can edit this paste and save as new: