标题: 有谁会做 [打印本页] 作者: 看好时机向前冲 时间: 2016-3-18 17:57 标题: 有谁会做 使用TCP协议写一个可以上传文件的服务器和客户端作者: 吃肉的小绵羊 时间: 2016-3-18 19:08
class TcpClient
{ public static void main (String [] args) throws Exception
{ Socket s = new Socket("192.168.1.255",10004);
OutputStream out= s.getOutStream();
out.write("服务端你好,我来了");
InputSteram in = new InputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}
class TcoServert
{ public static void main (String [] args) throws Exception
{ ServerSocket ss= new ServerSocket(10004);
Socket s= ss.accept();
String ip = s.getInetaddress().getHostAddress();
System.out.println(ip+"...connection");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
OutputStram out = s.getOutputStream();
out.write("我收到服务器的回答了".getBYtes());
s.close();
ss.close();
}
}作者: 吃肉的小绵羊 时间: 2016-3-18 19:09
getBYtes有个字母大写了,没看到,你改下y作者: 吃肉的小绵羊 时间: 2016-3-18 19:47
验证完的贴出来了
import java.io.*;
import java.net.*;
class TcpClient
{ public static void main (String [] args) throws Exception
{ Socket s = new Socket("192.168.1.255",10004);
OutputStream out= s.getOutputStream();
out.write("服务端你好,我来了".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}
class TcoServert
{ public static void main (String [] args) throws Exception
{ ServerSocket ss= new ServerSocket(10004);
Socket s= ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connection");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
OutputStream out = s.getOutputStream();
out.write("我收到服务器的回答了".getBytes());
s.close();
ss.close();
}
}