码迷,mamicode.com
首页 > 编程语言 > 详细

Java网络编程(ServerSocket和Socket)

时间:2014-08-16 09:48:40      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:style   java   os   io   ar   line   new   服务器   

服务器端


package org.tcp;


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


public class HelloServer {
public static void main(String[] args) throws Exception{
ServerSocket server = null;
Socket client = null;
PrintStream out = null;
server = new ServerSocket(8888);
System.out.println("服务器与性,等待客户端链接");
client = server.accept();
String str = "hello world!!!";
out = new PrintStream(client.getOutputStream());
out.println(str);
out.close();
client.close();
server.close();
}
}


程序运行结果:服务器运行,等待客户端链接


客户端


package org.tcp;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;


public class HelloClient {
public static void main(String[] args) throws Exception{
Socket client = null;
client = new Socket("localhost",8888);
BufferedReader buf = null;
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = buf.readLine();
System.out.println("服务器端输出内容:"+str);
client.close();
buf.close();
}
}


程序运行结果:服务器端输出内容:hello world!!!

Java网络编程(ServerSocket和Socket),布布扣,bubuko.com

Java网络编程(ServerSocket和Socket)

标签:style   java   os   io   ar   line   new   服务器   

原文地址:http://blog.csdn.net/u012927417/article/details/38597993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!