当前位置 : 主页 > 编程语言 > c++ >

简单的学生管理系统之网络聊天

来源:互联网 收集:自由互联 发布时间:2021-07-03
服务器发送和接收部分 package com.xll.chat;import java.io.DataInput;import java.io.DataInputStream;import java.io.IOException;import java.net.Socket;public class ServerGet extends Thread {static DataInputStream input=null;static So
服务器发送和接收部分
package com.xll.chat;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class ServerGet extends Thread {
	static DataInputStream input=null;
	static Socket socket=null;
	public ServerGet(Socket socket){
		this.socket=socket;
		
	}
	
	public void run(){
		try {
			input=new DataInputStream(socket.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		while(true){
			try {
				System.out.println(input.readUTF());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}






package com.xll.chat;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class ServerSend extends Thread{
	static Scanner inputs=new Scanner(System.in);
	static DataOutputStream out=null;
	static Socket sockek=null;
	public ServerSend(Socket socket){
		this.sockek=socket;
	}
	public int run(String name){
		try {
			String message=inputs.next();
			if(message=="0"){
				return 1;
			}else{
				out=new DataOutputStream(sockek.getOutputStream());
				out.writeUTF("好友"+name+"给你发来消息:"+message);
				return 2;
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
网友评论