自己去研究吧,就算我给你贴代码,你也是晕的,而且这种代码的重复使用性过低。 最简单如下吧 server端 public class shiyan { ServerSocket ss = null; Socket s =null; String str = null; public void fun() throws IOException{ ss = new ServerSocket(1008); while(true){ s =ss.accept(); DataInputStream b = new DataInputStream(s.getInputStream()); System.out.println("hehe"); str = b.readUTF(); System.out.println(str); }} public static void main(String[] args) throws IOException { new shiyan().fun(); }}client端 import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class yanshi { String str; Socket s =null; public void fun() throws IOException, IOException{ s = new Socket("127.0.0.1" , 1008); DataInputStream a =new DataInputStream(System.in); str = a.readUTF(); DataOutputStream b = new DataOutputStream(s.getOutputStream()); b.writeUTF(str); b.flush(); } public static void main(String[] args) { try { new yanshi().fun(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}
最简单如下吧
server端
public class shiyan {
ServerSocket ss = null;
Socket s =null;
String str = null;
public void fun() throws IOException{
ss = new ServerSocket(1008);
while(true){
s =ss.accept();
DataInputStream b = new DataInputStream(s.getInputStream());
System.out.println("hehe");
str = b.readUTF();
System.out.println(str);
}}
public static void main(String[] args) throws IOException {
new shiyan().fun();
}}client端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class yanshi {
String str;
Socket s =null;
public void fun() throws IOException, IOException{
s = new Socket("127.0.0.1" , 1008);
DataInputStream a =new DataInputStream(System.in);
str = a.readUTF();
DataOutputStream b = new DataOutputStream(s.getOutputStream());
b.writeUTF(str);
b.flush();
}
public static void main(String[] args) {
try {
new yanshi().fun();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}