Tcp网络编程练习题

1.使用TCP网络编程,实现图片的上传。

zy1c类:客户端

package week3.day3;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

//1、使用TCP网络编程,实现图片的上传。
public class zy1c {
    public static void main(String[] args) throws IOException {
        Socket c=new Socket("localhost",9990);
        FileInputStream in=new FileInputStream("src/-4cbe13e9bddede00.jpg");
        OutputStream out=c.getOutputStream();

        byte[] b=new  byte[1024];
        int i=in.read(b);
        while (i!=-1){
            out.write(b,0,i);
            i=in.read(b);
        }
        System.out.println("客户端发送文件完毕");
        out.close();
        in.close();
        c.close();


    }
}

zy1s类 服务端

package week3.day3;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class zy1s {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket=new ServerSocket(9990);
        Socket s=serverSocket.accept();
        System.out.println("111");
        InputStream in=s.getInputStream();
        FileOutputStream out=new FileOutputStream("src/week3/day3"+ UUID.randomUUID()+".jpg");
        byte[] b=new byte[1024];
        int  i=in.read(b);
        while(i!=-1){
            out.write(b,0,i);
            i=in.read(b);
        }
        System.out.println("111");
        out.close();
        in.close();

        s.close();
        serverSocket.close();

    }
}

 

2.使用TCP网络编程,完成注册功能 客户端: 填写用户信息(用户编号,用户名,密码,姓名,手机号) 发送信息给服务器

服务器: 接收信息 保存新用户到user.txt中

zy2c类  客户端

package week3.day3;

import java.io.*;
import java.net.Socket;

//2、使用TCP网络编程,完成注册功能
//客户端:
//填写用户信息(用户编号,用户名,密码,姓名,手机号)
//发送信息给服务器
//
//服务器:
//接收信息
//保存新用户到user.txt中
public class zy2c {
    public static void main(String[] args) throws IOException {
        Socket c=new Socket("localhost",9998);

        PerSon s1=new PerSon(1,23321,3213,"SDD",344223234);
        OutputStream outputStream = c.getOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(outputStream);

        oos.writeObject(s1);

        oos.close();
        outputStream.close();
        c.close();
    }
}

 zy2类 服务端

package week3.day3;

import cs.demo5.Student;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class zy2s {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ServerSocket serverSocket=new ServerSocket(9998);
        Socket s=serverSocket.accept();
        System.out.println("111");
        FileInputStream in= (FileInputStream) s.getInputStream();

        ObjectInputStream ois=new ObjectInputStream(in);
        PerSon p=(PerSon) ois.readObject();
        try {
            while (p!=null){
                System.out.println(p);
                p= (PerSon) ois.readObject();
            }

        }catch (EOFException e){
            System.out.println("处理了eof异常");
        }
        File file = new File("src\\User.txt");
        FileOutputStream f = new FileOutputStream(file);
        byte[] bytes = p.toString().getBytes();
        f.write(bytes);
        ois.close();
        in.close();
        s.close();
    }
}

你可能感兴趣的:(java学习例题,网络,tcp/ip,java)