首页 > 代码库 > 网络编程小实例

网络编程小实例

//// TCP编程1:客户端给服务端发送信息。服务端输出此信息到控制台上

// 网络编程实际上就是Socket的编程

@Test

public void client1() {

// 1.创建一个Socket对象,指明ip地址和端口号

Socket socket = null;

// 2.通过socket.getOutputStream来创建一个outputstream的对象

OutputStream os = null;

try {

socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898);

os = socket.getOutputStream();

// 3.利用os.write(),把要传输的内容写出去

String str = "你好,我是客户端";

byte[] b = str.getBytes();

os.write(b);

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

// 4.关闭相应的流

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 server1() {

// 1.创建ServerSocket的对象来和客户端的接口建立连接

ServerSocket ss = null;

// 2.创建Socket对象来接收客户端发送的消息

Socket s = null;

// 3.创建输入流来读取

InputStream is = null;

try {

ss = new ServerSocket(9898);

s = ss.accept();

is = s.getInputStream();

// 4.读取的具体过程

byte[] b = new byte[20];

int len;

while ((len = is.read(b)) != -1) {

System.out.println(new String(b, 0, len));

}

System.out.println(

"接收到来自于" + s.getInetAddress().getHostAddress() + "的信息");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// 5.关闭流

finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

if (s != null) {

try {

s.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

if (ss != null) {

try {

ss.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

}

 

}

// TCP编程2:

// 客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送“已收到信息”给客户端

 

@Test

public void client2() {

Socket socket = null;

OutputStream os = null;

InputStream is = null;

try {

socket = new Socket(InetAddress.getLocalHost(), 9898);

os = socket.getOutputStream();

os.write("小贝呀,是不是又偷吃糖葫芦了?".getBytes());

socket.shutdownOutput();

 

is = socket.getInputStream();

byte[] b = new byte[20];

int len;

while((len = is.read(b)) != -1){

System.out.print(new String(b,0,len));

}

} 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 server2() {

ServerSocket ss = null;

Socket s = null;

InputStream is = null;

OutputStream os = null;

try {

ss = new ServerSocket(9898);

s = ss.accept();

is = s.getInputStream();

byte[] b = new byte[20];

int len;

while ((len = is.read(b)) != -1) {

System.out.print(new String(b, 0, len));

}

System.out

.println("接收到来自" + s.getInetAddress().getHostAddress() + "的信息");

os = s.getOutputStream();

os.write("没有啊!!!不信你问我嫂子".getBytes());

os.flush();

} 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(s != null){

try {

s.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

if(ss != null){

try {

ss.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

}

}

 

网络编程小实例