本帖最后由 李大伟 于 2013-6-1 11:53 编辑
客户端和服务端写到一个.java文件里边,或者分开写成两个,能用MyEclipse运行么,还是只能通过命令行运行
大家帮忙看下,自己研究研究搞定了,分成两个运行就成了- /*
- */
- import java.net.*;
- import java.io.*;
- class TextClient
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("192.168.2.102",10006);
- BufferedReader bufr =
- new BufferedReader(new FileReader("IPDemo.java"));
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- //long time = System.currentTimeMills();
- //out.println(time);
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- out.println(line);
- }
- s.shutdownOutput();//关闭客户端输出流,相当给流中加入结束标记
- //out.println("over");
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufIn.readLine();
- sop(str);
- bufr.close();
- s.close();
- }
-
- }
- class TextServer
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10006);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- sop(ip+"....connected");
-
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter out = new PrintWriter(new FileWriter("server.txt"),true);
- String line = null;
- while((line=bufIn.readLine())!=null)
- {
- //if("over".equals(line))
- //break;
- out.println(line);
- }
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("上传成功,真爽!");
- out.close();
- s.close();
- ss.close();
- }
- }
复制代码 |