补充:网络编程tcp(二)
package com;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class TestTcp1 {
//客户端
@Test
public void client(){
Socket socket=null;
OutputStream os=null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
os = socket.getOutputStream();
os.write("我是1号客户,请多关照".getBytes());//向外输出数据
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket server=null;
Socket socket=null;
InputStream is=null;
try {
server=new ServerSocket(9000);
socket=server.accept();//accept拿到客户端socket对象
is=socket.getInputStream();//向程序里写入数据
byte[] b=new byte[20];
int len;
while((len=is.read(b))!=-1){
String str=new String(b,0,len);
System.out.println(str);
}
System.out.println("小伙子:收到来自"+socket.getLocalAddress());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(server!=null){
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
上例是只能客户端发送,服务器接收。下面的例子是服务器和客户端交互。有问有答。
package com;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
/**
* 案例:客户端给服务器发送消息,服务器打印信息。同时发出“已收到信息”给客户
* @author Administrator
*
*/
public class TestTcp2 {
//客户端
@Test
public void client(){
Socket socket=null;
OutputStream os=null;
InputStream is=null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
os = socket.getOutputStream();
os.write("wo shi kehu 2".getBytes());//向外输出数据,供服务器读取
socket.shutdownOutput();//显示告知,服务端“发送完了!!”,
is=socket.getInputStream();
byte[] b=new byte[50];
int len;
while((len=is.read(b))!=-1){
String str=new String(b,0,len);
System.out.println(str);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket server=null;
Socket socket=null;
InputStream is=null;
OutputStream os=null;
try {
server=new ServerSocket(9000);
socket=server.accept();//accept拿到客户端socket对象
is=socket.getInputStream();//拿到客户端的socket的数据向程序里写入数据
byte[] b=new byte[20];
int len;
while((len=is.read(b))!=-1){
String str=new String(b,0,len);
System.out.println("fuwqi:收到了:"+socket.getLocalAddress()+str);
}
//然后服务器说了,我收到信息我也给你回馈
os=socket.getOutputStream();
os.write("shou dao ni de xinxile ,yihouhuihaohao guanzhao ni de ".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(server!=null){
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}