import java.net.*;
import java.io.*;
public class Homework9_server
{
public static void main(String[] args)
{
try
{
System.out.println("server");
ServerSocket ss=new ServerSocket(6000);
Socket s=ss.accept();
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
byte[] buf=new byte[100];
int len=is.read(buf);
String str=new String(buf,0,len);
System.out.println("从客户那里得到: "+str);
if(str.equals("hollo server"))
os.write("what's your name".getBytes());
Thread.sleep(1000);
byte[] buf1=new byte[100];
int len1=is.read(buf1);
System.out.println(len1);
String str1=new String(buf1,0,len1);
File f=new File("answers.txt");
RandomAccessFile raf=new RandomAccessFile(f,"r");
String str2=raf.readLine();
if(str2.startsWith("client_name")&&str2.endsWith(str1))System.out.println("ok");
os.close();
is.close();
ss.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class Homework9_client
{
public static void main(String[] args)
{
System.out.println("client");
try
{
Socket s=new Socket(InetAddress.getByName(null),6000);
OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
os.write("hollo server".getBytes());
byte[] buf=new byte[100];
int len=is.read(buf);
is.read(buf);
String str=new String(buf,0,len);
System.out.println("从服务器获得: "+str);//就是这里过去不
Thread.sleep(1000);
if(str.equals("what's your name"))
{
File f=new File("questions.txt");
RandomAccessFile raf=new RandomAccessFile(f,"r");
String temp;
String[] templine={"1","2","3"};
while((temp=raf.readLine())!=null)
{
templine=temp.split(" ");
if(templine[0].equals("student_name"))
os.write(templine[2].getBytes());
}
}
os.close();
is.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
里面有一个文件流
流内容大家可能根据程序能看出来
长是有点长
可是为什么传不过去呢
这东西刚学
不太懂
挺幼稚的