qq_34722343
Devi_cry
2016-06-27 03:20

JAVA基础关于Scoket与GUI的编程问题,十万火急!!!!!!!!!!

5

我用Scoket编了HELLO WORLD 级别的服务端与客户端

 import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8686);
            Socket client = server.accept();

            InputStream is = client.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            OutputStream os = client.getOutputStream();
            PrintStream ps = new PrintStream(os);
            Scanner scanner = new Scanner(System.in);

            boolean flag = true;
            while (flag) {
                String s = br.readLine();
                System.out.println(s);
                if ( s.equals("ok")) {
                    System.out.println("连接结束");
                    break;
                }

                String s2 = scanner.nextLine();
                ps.println(s2); 
            }
            client.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
 import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        try {
            Socket client = new Socket("192.168.1.113", 8686);// IP地址与端口

            InputStream is = client.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));

            OutputStream os = client.getOutputStream();
            PrintStream ps = new PrintStream(os);
            Scanner scanner = new Scanner(System.in);
            boolean flag = true;
            while (flag)
            {
                String s2 = scanner.nextLine();
                ps.println(s2);
                if ( s2.equals("end")) {
                    System.out.println("连接结束");
                    break;
                }

                String s = bf.readLine();
                System.out.println(s);
            }

            client.close();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

想设计一个界面 ,图片说明

可是总有各种各样的问题,希望哪个好心人能教教我

在此感谢,

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

8条回答

为你推荐

换一换