网络编程:java.net.ConnectException: Connection refused: connect

该文章展示了一个JavaTCP网络编程的例子,通过socket模拟客户端和服务器端的通信。首先启动服务器端监听9090端口,然后客户端连接到该服务器,读取本地文件bz2.jpg并将其内容发送到服务器。服务器端接收到数据后,将内容写入到bz10.jpg文件中。
摘要由CSDN通过智能技术生成

场景:

使用tcp网络编程时,使用socket模拟,一个作为客户端,一个作为服务器接收端

原因:

必须先开启服务端,才可以开启客户端

就是先启动服务器端的@Test,然后再启动客户端的@Test

代码如下:

客户端:

package socket;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 TCP2 {
	
	@Test
	public void client() {
		
		FileInputStream fis =null;
		InetAddress inet=null;
		Socket socket = null;
		OutputStream os =null;
		//创建输入流:
		try {
			fis = new FileInputStream("src/bz2.jpg");
			inet = InetAddress.getByName("127.0.0.1");
			socket = new Socket(inet,9090);
			//获取输出流
		    os = socket.getOutputStream();
		    
		    //开始输出
		    byte[] cb=new byte[1024];
		    int len=0;
		    
		    while((len=fis.read(cb))!=-1){
		    	
		    	os.write(cb,0,len);
		    }
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 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) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            
            if(fis!=null) {
            	
            	try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        }
		
		
		
	}	

}

服务器端:先启动这个

package socket;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

public class Tcp2Server {
	
	@Test
	public void server() {
			
		ServerSocket serverSocket =null;
		Socket socket = null;
		InputStream is=null;
		FileOutputStream fos=null;
		try {
			 serverSocket = new ServerSocket(9090);
			//表示接受
			 socket = serverSocket.accept();
			//输入流
			 is = socket.getInputStream();
			 fos=new FileOutputStream("src/bz10.jpg");
			 byte[] buffer=new byte[1024];
			 int len=0;
			 while((len=is.read(buffer))!=-1) {
				 
				 fos.write(buffer, 0, len);
				  
			 }
			 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			
//			ServerSocket serverSocket =null;
//			Socket socket = null;
//			InputStream is=null;
//			FileOutputStream fos=null;
			
			if(is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(fos!=null) {
				try {
					fos.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(serverSocket!=null) {
				try {
					serverSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值