`
andesen
  • 浏览: 6169 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

socket send receive

    博客分类:
  • Java
 
阅读更多
	client
	public void createConnection() throws UnknownHostException, RefusedException, SocketTimeoutException
	{
		try {
			socket = new Socket();
			SocketAddress address =new  InetSocketAddress(this.hostUri, hostPort);
			socket.connect(address, 5000);
			logger.info("Socket server "+hostUri+" is connected");
		}catch (UnknownHostException e) {
			e.printStackTrace();
			throw new UnknownHostException();
		}catch(SocketTimeoutException e)
		{
			e.printStackTrace();
			throw new SocketTimeoutException();
		}
		catch (IOException e) {
			e.printStackTrace();
			throw new RefusedException();
		}
	}
	public boolean send()throws UnknownHostException,RefusedException, SocketTimeoutException ,SendException 
	{
		boolean result = false;
		createConnection();
		writer = getWriter();
		reader = getReader();
		try {
			writer.writeUTF(fileName);
			writer.flush();
			writer.writeLong(size);
			writer.flush();
			write(writer, reader);
			writer.flush();
			
			BufferedReader r = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			String line="";
			while (true) {
				line = r.readLine();
				if (line.equals("End"))
				{
					result =true;
					break;
				}
			}
		}
		catch (IOException e) {
			e.printStackTrace();
			throw new SendException();
		}finally
		{
			close();
		}
		return result;
	}
	
	protected void write(DataOutputStream dos,DataInputStream dis) throws IOException
	{
		byte []  buf  =new byte [2048];
		int read = 0;
		while((read=dis.read(buf))!=-1)
		{
			dos.write(buf,0,read);
		}
	}
	
	
	protected DataOutputStream getWriter()
	{
		try {
			writer = new DataOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this.writer;
	}
	protected DataInputStream getReader()
	{
		return this.reader;
	}
	
	protected void close()  {
		try {
			reader.close();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
server
public void run() {
			String filePath="";
			try {
				dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
				String fileName = dis.readUTF();
				if(fileName.toLowerCase().indexOf(".jar")>0)
				{
					filePath=jarUploadPath+"//"+ fileName;
				}else
				{
					filePath=xmlUploadPath+"//"+ fileName;
				}
				Long lth = dis.readLong();
				File file = new File(filePath);
				dos = new DataOutputStream(new FileOutputStream(file));
				byte[] buf = new byte[1024];
				int read = 0;
				long tmp = 0;
				log.info("正在接收文件:"+filePath+",大小:"+lth+"kb");
				while ((read = dis.read(buf)) != -1) {
					dos.write(buf, 0, read);
					tmp+=read;
					if(lth==tmp)
					{
						break;
					}
				}
				PrintWriter pw  = new PrintWriter(socket.getOutputStream());
				log.info("文件接收完成:"+filePath);
				pw.write("End");
				pw.flush();
			} catch (IOException e) {
				log.error("文件接收错误:"+filePath);
				e.printStackTrace();
			} finally {
				try {
					dis.close();
					dos.flush();
					dos.close();
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} 
 
分享到:
评论

相关推荐

    Socket Send and Receive

    收发数据是网络编程的主题,在套接字上收发数据我们可以使用send和recv,当然还有Winsock2的WSASend和WSARecv。我们这里只讨论send和recv。

    TDC_SOCKET_Send&Receive.zip_TDC SOCKET_socket 重连_socket断线重连_数据通讯

    TDC与PC间的SOCKET以太网通讯测试程序,完美实现TDC与PC间的以太网SOCKET通讯,且断线自动重连,无须进行字节顺序变换完美传递数据。

    基于socket的简单通讯工具

    简单的C#Socket工具,Socket.Connect 方法:建立到远程设备的连接 public void Connect(EndPoint remoteEP)(有重载方法) Socket.Send 方法:从数据中的指示位置开始将数据发送到连接的 Socket。 public int Send...

    c# 简单的Socket聊天工具

    服务器上等待的socket获取连接的socket后,通过Receive方法来获取客服端A的请求内容,此时处于等待状态,只到客服端A使用send方法发送信息为止。Receive方法将通信的内容获得。这就完成一次通信

    socket监听工具

    For each created socket, the following information is displayed: socket handle, socket type, local and remote addresses, local and remote ports, total number of send/receive bytes, and more....

    通过C#实现局域网聊天室-实现服务器端程序,客户端程序

    C#可以通过Socket编程实现...在新线程中,使用Socket的Receive和Send方法接收和发送消息。 客户端程序: 创建一个客户端Socket对象,连接服务器的IP地址和端口号。 使用Socket的Send和Receive方法发送和接收消息。

    src.zip_Listen!_ReceiveFrom_socket绑定端口

    d.TCP情况下,如果监听到一个连接,就可以使用accept来接收这个连接,然后就可以利用Send/Receive来执行操作了。而UDP,则不需要accept,直接使用SendTo/ReceiveFrom来执行操作。(注意,因为UDP不需要建立连接,...

    Socket-类封装

     2)TCPClient类的send/receive方法使用了著名的writen/readn(来源UNP)实现, 解决了TCP的粘包问题.  3)TCPServer端添加了地址复用, 可以方便TCP服务器重启;  4)添加了异常类,让我们在编写易出错的代码时,可以解放...

    c_socket编程入门

    可见,在应用程序端或者服务器端创建了Socket对象之后,就可以使用Send/SentTo方法将数据发送到连接的Socket,或者使用Receive/ReceiveFrom方法接收来自连接Socket的数据; 针对Socket编程,.NET 框架的 Socket ...

    Socket 类封装 改进版

    实现中的几个注意点: 1 Socket类几个成员函数的访问权限为protected 使Socket类可以进行继承 但不允许私自使用... 2 TCPClient类的send receive方法使用了著名的writen readn 来源UNP 实现 解决了TCP的粘包问题 3 TCPSe

    socket通信技术

    1、 程序:socket_Send为发送端; 程序:socket_Receive为接收端; 2、 此程序可以用来接收和发送txt,jpg.avi等各种文件和char类型的数据; 如果对于结构体,可以先将其转化成char,再发送,接收之后,再转化成...

    c#基于Socket客户端服务端通信聊天

    至于Send和Receive,则由NetworkStream进行处理。 2.TcpClient(为 TCP 网络服务提供客户端连接) Connect:有两种方法,一种是使用无参构造方法创建TcpClient,然后调用Connect方法;另一种是使用带host与port的构造...

    socket套接字编程

    1、熟悉socket编程接口,初步掌握用socket编程接口开发面向连接的网络应用程序的方法,并且能较好的理解、掌握...4、掌握socket地址及其操作并且掌握socket的基本函数如socket、bing、listen、accept、send、receive等

    socketsniff

    For each created socket, the following information is displayed: socket handle, socket type, local and remote addresses, local and remote ports, total number of send/receive bytes, and more....

    Socket实现的基本Tcp通信

    开发环境:VC++ 6.0 实现了Socket收发包基本流程,Client和Server合一 ...Server端:create,listen,accept,receive,send Client端:create,connect,receive,send 可以直接运行程序,或者编译运行

    socket编程,CS间相互通讯(含实验报告)

    实验要求如下: 1)设计程序,分别构建通信的两端:服务器端和客户端应用... 6,注意理解程序的线程、Socket的基本动作(Accept、Connect、Send、Receive)等; 7,本实验务必独立完成,后续的实验将以此为基础拓展;

    Socket通信实例代码

    TCP情况下,如果监听到一个连接,就可以使用accept来接收这个连接,然后就可以利用Send/Receive来执行操作了。而UDP,则不需要accept, 直接使用SendTo/ReceiveFrom来执行操作。(看清楚哦,和TCP的执行方法有区别...

    ftp客户端ftpclient纯C语言winsock实现socket编程

    if(r2==SOCKET_ERROR){printf("send file error!");closesocket(ds);return -1;} bsnum=bsnum+r2; r3=r3-r2; printf("send %8d bytes! have sended %16d bytes!\r",r2,bsnum); }while(brnum>bsnum); ...

    VC MFC Socket编程.rar

    VC MFC Socket编程,这里面不至一个例子,最后做出了一个简单的MFC Socket 聊天室例子,来进一步演示MFC Socket编程的技术细节,对于VC 初学者是非常不错的参考范例。  CDialog::OnInitDialog();  CSocket sock...

    tcp收发程序(C#)

    This example shows how to send and receive data via TCP/IP using Socket in .NET Framework. There are methods Socket.Send and Socket.Receive

Global site tag (gtag.js) - Google Analytics