`
heisalittlebird
  • 浏览: 19511 次
文章分类
社区版块
存档分类
最新评论

java 聊天

阅读更多
package com.phj20110829.net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

public class Chat {
	public static void main(String[] args) {
		new Thread(new SenderThread()).start();
	
		new Thread(new ReceiverThread()).start();
	}
}

class SenderThread implements Runnable {

	@Override
	public void run() {

		OutputStream os = null;
		DataOutputStream dos = null;

		try {
			while (true) {
				String msg = JOptionPane.showInputDialog("Client,请输入一句话:");
				if (msg.equals("bye")) {
					System.out.println("The client have quited");
					break;
				}
				Socket client = new Socket("localhost", 8099);
				// 向服务器端发送数据
				os = client.getOutputStream();// 发送数据
				dos = new DataOutputStream(os);
				dos.writeUTF("Client:" + msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				os.close();
				dos.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
	}

}

class ReceiverThread implements Runnable {

	@Override
	public void run() {
		try {
			ServerSocket server = new ServerSocket(8099);
			while (true) {
				Socket client = server.accept();
				
				// 得到客户端发送的数据
				InputStream is = client.getInputStream();// 得到数据
				DataInputStream dis = new DataInputStream(is);
				System.out.println(dis.readUTF());				

				is.close();				
				dis.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}


package com.phj20110829.net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

public class ServerSocketDemo {
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(8088);
		while (true) {
			
			Socket client = server.accept();
			
			//得到客户端发送的数据
			InputStream is = client.getInputStream();// 得到数据
			DataInputStream dis = new DataInputStream(is);

			System.out.println(dis.readUTF());

			// 向客户端发送数据
			OutputStream os = client.getOutputStream();// 发送数据
			DataOutputStream dos = new DataOutputStream(os);
			String msg=JOptionPane.showInputDialog("Sever,请输入一句话:");
			dos.writeUTF("Server:"+msg);

			is.close();
			os.close();
			dis.close();
			dos.close();

		}
	}
}


package com.phj20110829.net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
//import java.util.Scanner;

import javax.swing.JOptionPane;

public class SocketDemo {

	public static void main(String[] args) throws Exception {

		//Scanner in = new Scanner(System.in);

		OutputStream os = null;
		DataOutputStream dos = null;
		InputStream is = null;
		DataInputStream dis = null;

		while (true) {
			
			//String msg = in.next();
			String msg=JOptionPane.showInputDialog("Client,请输入一句话:");
			if (msg.equals("bye")) {
				System.out.println("The client have quited");
				break;
			}
			
			//new一次就等于和server建立了连接
			Socket client = new Socket("localhost", 8088);			
			
			// 向服务器端发送数据
			os = client.getOutputStream();// 发送数据
			dos = new DataOutputStream(os);
			dos.writeUTF("Client:"+msg);

			//得到服务器端发送过来的数据
			is = client.getInputStream();//得到数据
			dis = new DataInputStream(is);
			System.out.println(dis.readUTF());

		}
		
		if (is != null) {
			is.close();
		}
		if (os != null) {
			os.close();
		}
		if (dis != null) {
			dis.close();
		}
		if (dos != null) {
			dos.close();
		}
		
		
		
		

	}
}


package com.phj20110829.net;

import java.net.InetAddress;

public class InnetAddressDemo {
	public static void main(String[] args) throws Exception {
		InetAddressFun();
	}
	public static void InetAddressFun()throws Exception{
		InetAddress innet = InetAddress.getByName("tanlan");
		System.out.println(innet);
		System.out.println(InetAddress.getLocalHost());;
	}
	
}


package com.phj20110829.net;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class NetDemo {
	public static void main(String[] args) throws Exception {
		spider("http://www.baidu.com");
	}
	public static void spider(String address) throws Exception{
		URL url= new URL(address);
		URLConnection urlconn = url.openConnection();
		
		InputStream is = urlconn.getInputStream();
		OutputStream os = new FileOutputStream("d://index.html",true);
		byte[] b = new byte[1024];
		int i=0;
		while((i=is.read(b,0,b.length))!=-1){
			os.write(b);
		}
		
		is.close();
		os.close();
	}
//over
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics