一、实验目的:
二、实验环境:
NetBeans IDE 8.2
三、实验内容:
(写出主要的内容)
1、使用URL读取百度主页并在控制台打印输出。
import java.net.*;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
URL url;
public Main(URL url) throws UnsupportedEncodingException, IOException {
this.url = url;
get_content();
}
private void get_content() throws UnsupportedEncodingException, IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
String s = null;
while((s = br.readLine())!=null){
System.out.println(s);
}
}
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
new Main(new URL("http://www.baidu.com"));
}
}
2、使用TCP套接字读取服务器端文件,并在控制台打印输出。
i
mport java.io.*;
import java.net.*;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.1.115", 11111);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
BufferedReader br = new BufferedReader(new FileReader("ip.java"));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
s.close();
}
}
import java.io.*;
import java.net.*;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(11111);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
3、实现一个命令行界面的C/S聊天室程序,服务器应包含多个线程,每个Socket对应一个线程,该线程负责读取Socket对应输入流的数据(从客户端发送过来数据),并将读到的数据向每个Socket输出流发送一次(将一个客户端发送数据“广播”到其他的客户端),提示:需要在服务器端使用List来保存所有的Socket。
import java.io.*;
import java.net.*;
import java.util.*;
public class javaSocket {
public static List<Socket> socketList = Collections.synchronizedList(new ArrayList<Socket>());
public static void main (String [] args) throws IOException {
ServerSocket ss = new ServerSocket(30000);
while(true)
{
Socket s = ss.accept();
socketList.add(s);
new Thread( new ServerThread(s)).start();
}
}
}
import java.io.*;
import java.net.*;
public class ServerThread implements Runnable {
Socket s = null;
BufferedReader br = null;
public ServerThread(Socket s){
this.s = s ;
try {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));//创建并读取流
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run()
{
try
{
String content = null;
while ((content = readFromClient())!=null)
{
for(Socket s:javaSocket.socketList)
{
PrintStream ps = new PrintStream(s.getOutputStream());
}
}
}catch (IOException e)
{
e.printStackTrace();
}
}
private String readFromClient() {
try
{
return br.readLine();
}catch (IOException e) {
e.printStackTrace();
javaSocket.socketList.remove(s);//移除socketList中的数据三
}
return null;
}
}
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String [] args) throws IOException
{
Socket s = new Socket("127.0.0.1",30000);
new Thread( new ClientThread(s)).start();
PrintStream ps = new PrintStream(s.getOutputStream());
String line = null;
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
while ((line = br.readLine())!=null)
{
ps.println(line);
}
}
}
import java.io.*;
import java.net.*;
public class ClientThread implements Runnable {
private Socket s ;
BufferedReader br = null;
public ClientThread(Socket s) throws IOException {
this.s = s ;
br = new BufferedReader(new InputStreamReader(s.getInputStream()))
}
@Override
public void run()
{
try
{
String content = null;
while ((content = br.readLine())!=null)
{
System.out.println(content);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
多次重复本文仅供参考学习,全文复制粘贴者后果自负;
博主写了这么多篇文章作业的目的在于提供给一个参考资料,不是抄袭模板,少浪费时间在于解决无意义的bug,多花时间往后面学学;
推荐歌曲:星空–-五月天