如何使用java中的套接字编程在TCP协议中通过网络发送数据(特定字符串)? [英] How do I send data(strings in particular) over network in TCP protocol using socket programming in java?

查看:78
本文介绍了如何使用java中的套接字编程在TCP协议中通过网络发送数据(特定字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想要一个程序来读取文本文件(客户端机器),然后将其内容发送到服务器程序,并将其存储在sql数据库中



任何建议我怎么能实现这个东西



谢谢



我尝试过:



客户



actually i want a program to read a text file(client machine) and then send its contents to a server program where it stores it in a sql database

any suggestions how i can i implement this things

thanks

What I have tried:

Client

import java.io.File;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.*;
import java.io.*;


public class Client { 
    
    public static void main(String[] args) throws Exception,IOException{
        
        //Initialize socket
        Socket socket = new Socket(InetAddress.getByName("LINUXSERVER"), 5000);
        byte[] contents = new byte[10000];
        File file = new File("d:\\test.txt");
        Scanner fis = new Scanner(file);
        //BufferedInputStream bis = new BufferedInputStream(fis); 
        String temp,temp1="";
        Vector v = new Vector();
          while(fis.hasNext()){
              temp = fis.next();
              for(int i =0;i<temp.length();i++){
                  if(temp.charAt(i)!=',' && temp.charAt(i)!=';'){
                      temp1 = temp1+temp.charAt(i);
                  }
                  else{
                      v.addElement(temp1);
                  }
              }
          }
        //Get socket's output stream
        OutputStream osw = socket.getOutputStream();
                Iterator itr = v.iterator();
                while(itr.hasNext()){
                    String temp2 = String.valueOf(itr.next());
                    byte senddata[] = temp2.getBytes("UTF-8");
                    System.out.print(senddata.toString());
                }
        //Read File Contents into contents array 
        //byte[] contents;
        /*long fileLength = file.length(); 
        long current = 0;
         
        long start = System.nanoTime();
        while(current!=fileLength){ 
            int size = 10000;
            if(fileLength - current >= size)
                current += size;    
            else{ 
                size = (int)(fileLength - current); 
                current = fileLength;
            } 
            contents = new byte[size]; 
            //bis.read(contents, 0, size);
            os.write(contents);
            System.out.println("Sending file ... "+(current*100)/fileLength+"% complete!");
        }   
        */
        osw.flush(); 
        //File transfer done. Close the socket connection!
        socket.close();
        System.out.println("File sent succesfully!");  
       
       
    }
}





服务器





Server

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Server1 { 
    
    public static void main(String[] args) throws Exception 
    {
        //Initialize Sockets
        ServerSocket ssock = new ServerSocket(5000);
        System.out.println("Server Started");
        System.out.println("Waiting For Client...");
        Socket socket = ssock.accept();
        System.out.println("Client Connected Now Recieveing..");
        //The InetAddress specification
        InetAddress IA = InetAddress.getByName("192.168.0.110"); 
              byte[] contents = new byte[10000];
        //Initialize the FileOutputStream to the output file's full path.
        FileWriter fos = new FileWriter("d:\\1\\test.txt");
        BufferedWriter bw = new BufferedWriter(fos);
        PrintWriter pw = new PrintWriter(bw);
        //BufferedOutputStream bos = new BufferedOutputStream(fos);
        InputStream is = socket.getInputStream();
        
        //No of bytes read in one read() call
        int bytesRead = 0; 
        
        while((bytesRead=is.read(contents))!=-1){
            //bos.write(contents, 0, bytesRead); 
            pw.write(String.valueOf(bytesRead));
        }
        pw.flush();
        //bos.flush(); 
        
        socket.close(); 
        ssock.close();
        System.out.println("File saved successfully!");
    }
}



i没有尝试sql数据库插入


i havent tried sql database insertion

推荐答案

这是常见的为这些任务定义协议。在您的情况下,我将首先发送一个至少包含字符串/文件长度的已定义标头。然后接收器将事先知道man字节是如何跟随的。其他(可选)标题字段可能是文件名,校验和,标识值和版本信息(允许更新协议)。



它也可能是没有必要对文件内容执行操作。只需按原样发送(二进制)。然后服务器首先读取标头并处理它。然后可以将以下数据写入缓冲区或文件,最后存储在数据库中。
It is common to define a protocol for such tasks. In your case I would send a defined header first that contains at least the string / file length. Then the receiver will know in advance how man bytes are following. Other (optional) header fields might be the file name, a checksum, an identification value, and a version information (allows updating the protocol).

It might be also not necessary to perform operations on the file content. Just send it as is (binary). The server is then reading the header first and processes it. The following data can then be written to a buffer or file and finally stored in the database.


这篇关于如何使用java中的套接字编程在TCP协议中通过网络发送数据(特定字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆