服务器端
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])
{
ServerSocket server=null;
Socket you=null;
DataInputStream in=null;
DataOutputStream out=null;
try
{
server=new ServerSocket(4331);
}catch(IOException e)
{
System.out.println(e);
}
try{
System.out.println("等待客户呼叫");
you=server.accept();
out=new DataOutputStream(you.getOutputStream());
in =new DataInputStream(you.getInputStream());
while(true)
{
char c=in.readChar();
System.out.println("服务器收到:"+c);
out.writeChar((char)(c-32));
Thread.sleep(500);
}
}
catch(Exception e)
{
System.out.println("客户已断开"+e);
}
}
}
客户端
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String args[])
{
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try
{
mysocket=new Socket("172.23.36.242",4331);
out=new DataOutputStream(mysocket.getOutputStream());
in =new DataInputStream(mysocket.getInputStream());
char c='a';
while(true)
{
if(c>'z')
c='a';
out.writeChar(c);
char s=in.readChar();
System.out.println("客户收到:"+s);
c++;
Thread.sleep(500);
}
}catch(Exception e)
{
System.out.println(e);
}
}
}