Java语言进阶实验指导书 下载本文

class Thread1 extends Thread { public void run() { synchronized (object1) { // do something try {

Thread.sleep(100); }

catch (InterruptedException ex) { }

System.out.println(\

synchronized (object2) { // do something } } } }

class Thread2 extends Thread { public void run() {

synchronized (object2) { // do something try {

Thread.sleep(100); }

catch (InterruptedException ex) { }

System.out.println(\

synchronized (object1) { // do something } } } } }

实验三 Networking网络编程

一、实验目的:

1. 理解基于流与基于数据包的网络编程方式

2. 掌握应用socket建立CS应用程序的相关类的应用

二、实验内容:

Write a program that enables two users to chat. Implement one user as

the server (Figure 30.24(a)) and the other as the client (Figure 30.24(b)). The server has two text areas: one for entering text and the other (noneditable) for displaying text received from the client. When the user presses the Enter key,

the current line is sent to the client. The client has two text areas: one for receiving text from the server, and the other for entering text. When the user

presses the Enter key, the current line is sent to the server. Name the client Exercise30_12Client and the server Exercise30_12Server.

三、实验要求:

1、认真执行每一个步骤的,并作好记录。

2、实验报告中给出配置的详细步骤和重要的截图。

四、实验学时:2学时

五、实验步骤:

给定如下客户端的代码,请完成服务器端代码的编写。 // Exercise30_12Client.java import java.io.*; import java.net.*; import java.util.*; import java.awt.*;

import java.awt.event.*; import javax.swing.*;

import javax.swing.border.*;

public class Exercise30_12Client extends JFrame { // Text area for entering server text

private JTextArea jtaServer = new JTextArea();

// Text area for displaying client text

private JTextArea jtaClient = new JTextArea();

private PrintWriter output;

public static void main(String[] args) { new Exercise30_12Client(); }

public Exercise30_12Client() { // Place text area on the frame setLayout(new GridLayout(2, 1));

JScrollPane jScrollPane1 = new JScrollPane(jtaServer); JScrollPane jScrollPane2 = new JScrollPane(jtaClient); jScrollPane1.setBorder(new TitledBorder(\ jScrollPane2.setBorder(new TitledBorder(\ add(jScrollPane2, BorderLayout.CENTER); add(jScrollPane1, BorderLayout.CENTER);

jtaServer.setWrapStyleWord(true); jtaServer.setLineWrap(true);

jtaClient.setWrapStyleWord(true); jtaClient.setLineWrap(true); // jtaClient.setEditable(false);

setTitle(\ setSize(500, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // Center the frame

setVisible(true); // It is necessary to show the frame here!

try {

// Create a server socket

Socket connectToServer = new Socket(\

output = new

PrintWriter(connectToServer.getOutputStream()); new SendThread(connectToServer).start(); new ReceiveThread(connectToServer).start(); }

catch(IOException ex) { System.err.println(ex);

}

jtaServer.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == e.VK_ENTER) { output.print(jtaServer.getText()); } } }); }

class SendThread extends Thread { SendThread(Socket socket) { // Keep sending to the client

} }

class ReceiveThread extends Thread { ReceiveThread(Socket socket) { // Keep receiving from the client

} } }