Java sockets, sending data to and from?

Alright, I'm trying to set a program I have to receive data from a socket, and send data to a socket. I'm stumped on how to get the client side of the socket to send specific data, then have the server side send specific data. Here's what I currently have, it's only my server-side because I'm really lost on the client part as of now. To further evaluate, I would like to do as listed in the following, yet I can't figure out what to research to write the client-side of the socket, and if there was any code I would need to rewrite in the server side? ![To further evaluate][1] package sockets; import*; import*; public class SocketMain { private int port = 0; public ServerSocket socket; public Socket clientSock; public SocketMain() { init(); } public static void main(String[] args) { new SocketMain(); } private void init() { try { socket = new ServerSocket(port); System.out.println("Server started, bound to port: "+port); clientSock = socket.accept(); File directory = new File("./Storage/"); if (!directory.exists()) { directory.mkdirs(); } File file = new File(directory + "/Store.dat"); if (!file.exists()) { file.createNewFile(); } DataInputStream in = new DataInputStream(clientSock.getInputStream()); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); String line; while ((line = in.readLine()) != null) { System.out.println(line); bw.write(line+"\n"); bw.flush(); bw.close(); } socket.close(); clientSock.close(); } catch (IOException ex) { ex.printStackTrace(); } } } [1]:
Why are you catching the IOException?

以上就是Java sockets, sending data to and from?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准