当前位置: 技术问答>java相关
100分求解TCP/IP C/S的例程!!
来源: 互联网 发布时间:2015-02-06
本文导语: | Client::: import java.net.*; import java.io.*; class SimpleClient { public static void main(String args[]) throws Exception { Socket s=new Socket("192.168.68.211",8888);//Socket客户端用它初始连接一次 InputStream is=s.getInputStream(); DataInputS...
|
Client:::
import java.net.*;
import java.io.*;
class SimpleClient
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("192.168.68.211",8888);//Socket客户端用它初始连接一次
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
System.out.println(dis.readUTF());
is.close();
s.close();
}
}
Server:::
import java.net.*;
import java.io.*;
class SimpleServer
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(8888);//ServerSocket用以侦听进入的socket连接
while(true)
{
Socket s=ss.accept();
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF("hello");
os.close();
s.close();
}
}
}
import java.net.*;
import java.io.*;
class SimpleClient
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("192.168.68.211",8888);//Socket客户端用它初始连接一次
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
System.out.println(dis.readUTF());
is.close();
s.close();
}
}
Server:::
import java.net.*;
import java.io.*;
class SimpleServer
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(8888);//ServerSocket用以侦听进入的socket连接
while(true)
{
Socket s=ss.accept();
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF("hello");
os.close();
s.close();
}
}
}