Guest User

Untitled

a guest
Jan 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. public class ft2client
  5. {
  6. public static void main(String srgs[])throws IOException
  7. {
  8. Socket s=null;
  9. BufferedReader get=null;
  10. PrintWriter put=null;
  11. try
  12. {
  13. s=new Socket("127.0.0.1",8085);
  14. get=new BufferedReader(new InputStreamReader(s.getInputStream()));
  15. put=new PrintWriter(s.getOutputStream(),true);
  16. }
  17. catch(Exception e)
  18. {
  19. System.exit(0);
  20. }
  21. String u,f;
  22. System.out.println("Enter the file name to transfer from server:");
  23. DataInputStream dis=new DataInputStream(System.in);
  24. f=dis.readLine();
  25. put.println(f);
  26. File f1=new File(f);
  27. String str = "/home/user/";
  28. FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));
  29. while((u=get.readLine())!=null)
  30. {
  31. byte jj[]=u.getBytes();
  32. fs.write(jj);
  33. }
  34. fs.close();
  35. System.out.println("File received");
  36. s.close();
  37. }
  38. }
  39.  
  40. import java.io.*;
  41. import java.net.*;
  42. import java.util.*;
  43. public class ft2server
  44. {
  45. public static void main(String args[])throws IOException
  46. {
  47. ServerSocket ss=null;
  48. try
  49. {
  50. ss=new ServerSocket(8085);
  51. }
  52. catch(IOException e)
  53. {
  54. System.out.println("couldn't listen");
  55. System.exit(0);
  56. }
  57. Socket cs=null;
  58. try
  59. {
  60. cs=ss.accept();
  61. System.out.println("Connection established"+cs);
  62. }
  63. catch(Exception e)
  64. {
  65. System.out.println("Accept failed");
  66. System.exit(1);
  67. }
  68. PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
  69. BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
  70. String s=st.readLine();
  71. String str = "/home/user/Desktop/";
  72. String path = str + s;
  73. System.out.println("The requested file is path: "+path);
  74. System.out.println("The requested file is : "+s);
  75. File f=new File(path);
  76. if(f.exists())
  77. {
  78. BufferedReader d=new BufferedReader(new FileReader(s));
  79. String line;
  80. while((line=d.readLine())!=null)
  81. {
  82. put.write(line);
  83. put.flush();
  84. }
  85. d.close();
  86. System.out.println("File transfered");
  87. cs.close();
  88. ss.close();
  89. }
  90. }
  91. }
  92.  
  93. import java.io.*;
  94. import java.net.*;
  95. import java.util.*;
  96. public class ft2server
  97. {
  98.  
  99. public static void main(String args[])throws IOException
  100. {
  101. ServerSocket ss=null;
  102. try
  103. {
  104. ss=new ServerSocket(8085);
  105. }
  106. catch(IOException e)
  107. {
  108. System.out.println("couldn't listen");
  109. System.exit(0);
  110. }
  111. Socket cs=null;
  112. try
  113. {
  114. cs=ss.accept();
  115. System.out.println("Connection established"+cs);
  116. }
  117. catch(Exception e)
  118. {
  119. System.out.println("Accept failed");
  120. System.exit(1);
  121. }
  122. BufferedOutputStream put=new BufferedOutputStream(cs.getOutputStream());
  123. BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
  124. String s=st.readLine();
  125. String str = "/home/milind/Desktop/";
  126. String path = str + s;
  127. System.out.println("The requested file is path: "+path);
  128. System.out.println("The requested file is : "+s);
  129. File f=new File(path);
  130. if(f.isFile())
  131. {
  132. FileInputStream fis=new FileInputStream(f);
  133.  
  134.  
  135. byte []buf=new byte[1024];
  136. int read;
  137. while((read=fis.read(buf,0,1024))!=-1)
  138. {
  139. put.write(buf,0,read);
  140. put.flush();
  141. }
  142. //d.close();
  143. System.out.println("File transfered");
  144. cs.close();
  145. ss.close();
  146. }
  147. }
  148. }
  149.  
  150. import java.io.*;
  151. import java.net.*;
  152. import java.util.*;
  153. public class ft2client
  154. {
  155. public static void main(String srgs[])throws IOException
  156. {
  157. Socket s=null;
  158. BufferedInputStream get=null;
  159. PrintWriter put=null;
  160. try
  161. {
  162. s=new Socket("127.0.0.1",8085);
  163. get=new BufferedInputStream(s.getInputStream());
  164. put=new PrintWriter(s.getOutputStream(),true);
  165.  
  166. String f;
  167. int u;
  168. System.out.println("Enter the file name to transfer from server:");
  169. DataInputStream dis=new DataInputStream(System.in);
  170. f=dis.readLine();
  171. put.println(f);
  172. File f1=new File(f);
  173. String str = "/home/milind/";
  174. FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));
  175. byte jj[]=new byte[1024];
  176. while((u=get.read(jj,0,1024))!=-1)
  177. {
  178. fs.write(jj,0,u);
  179. }
  180. fs.close();
  181. System.out.println("File received");
  182. s.close();
  183. }catch(Exception e)
  184. {
  185. e.printStackTrace();
  186. System.exit(0);
  187. }
  188. }
  189. }
  190.  
  191. Server {
  192. ...
  193. soc = echoServer.accept();
  194. out = new DataOutputStream(soc.getOutputStream());
  195. out.flush();
  196. in = new DataInputStream(soc.getInputStream());
  197. out.writeBytes("some text in here n");
  198. out.flush();
  199.  
  200. ...
  201. }
  202.  
  203.  
  204. Client{
  205.  
  206. ...
  207.  
  208. soc = new Socket("10.210.13.121", 62436);
  209. out = new DataOutputStream(soc.getOutputStream());
  210. out.flush();
  211. in = new DataInputStream(soc.getInputStream());
  212. ...
  213. while(true)
  214. if(in.available() > 0)
  215. String str = in.readLine();
  216. ...
  217. }
  218.  
  219. import java.io.*;
  220. import java.net.*;
  221. import java.util.*;
  222. public class ft2server
  223. {
  224. public static void main(String args[])throws IOException
  225. {
  226. ServerSocket ss=null;
  227. try
  228. {
  229. ss=new ServerSocket(8085);
  230. }
  231. catch(IOException e)
  232. {
  233. System.out.println("couldn't listen");
  234. System.exit(0);
  235. }
  236. Socket cs=null;
  237. try
  238. {
  239. cs=ss.accept();
  240. System.out.println("Connection established"+cs);
  241. }
  242. catch(Exception e)
  243. {
  244. System.out.println("Accept failed");
  245. System.exit(1);
  246. }
  247. PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
  248. BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
  249. String s=st.readLine();
  250. String str = "/home/milind/Desktop/";
  251. String path = str + s;
  252. System.out.println("The requested file is path: "+path);
  253. System.out.println("The requested file is : "+s);
  254. File f=new File(path);
  255. if(f.isFile())
  256. {
  257. BufferedReader d=new BufferedReader(new FileReader(f));
  258. String line;
  259. while((line=d.readLine())!=null)
  260. {
  261. put.write(line);
  262. put.flush();
  263. }
  264. d.close();
  265. System.out.println("File transfered");
  266. cs.close();
  267. ss.close();
  268. }
  269. }
  270. }
  271.  
  272. import java.io.*;
  273. import java.net.*;
  274. import java.util.*;
  275. public class ft2client
  276. {
  277. public static void main(String srgs[])throws IOException
  278. {
  279. Socket s=null;
  280. BufferedReader get=null;
  281. PrintWriter put=null;
  282. try
  283. {
  284. s=new Socket("127.0.0.1",8085);
  285. get=new BufferedReader(new InputStreamReader(s.getInputStream()));
  286. put=new PrintWriter(s.getOutputStream(),true);
  287.  
  288. String u,f;
  289. System.out.println("Enter the file name to transfer from server:");
  290. DataInputStream dis=new DataInputStream(System.in);
  291. f=dis.readLine();
  292. put.println(f);
  293. File f1=new File(f);
  294. String str = "/home/milind/";
  295. FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));
  296. while((u=get.readLine())!=null)
  297. {
  298. System.out.println(u);
  299. byte jj[]=u.getBytes();
  300. fs.write(jj);
  301. }
  302. fs.close();
  303. System.out.println("File received");
  304. s.close();
  305. }catch(Exception e)
  306. {
  307. e.printStackTrace();
  308. System.exit(0);
  309. }
  310. }
  311. }
Add Comment
Please, Sign In to add comment