客户端代码
public class SocketTest {
public static void main(String[] args) {
Socket sk =null;
try {
Scanner input=new Scanner(System.in);
System.out.println("请输入内容,如果输入f,输入结束:");
String name=input.next();
while(!name.equals("f")){
sk=new Socket("127.0.0.1",8088);
OutputStream out =sk.getOutputStream();
PrintWriter os =new PrintWriter(out);
BufferedReader is =new BufferedReader(new InputStreamReader(sk.getInputStream()));
//想服务器传入数据
os.print(name);
os.flush();
//获取服务器返回的数据
String res=is.readLine();
System.out.println(res);
name=input.next();
//释放资源
os.close();
is.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sk!=null){
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}}服务器代码
public class ServerSocketTest extends HttpServlet {
public void init() throws ServletException{
super.init();
this.doLister();
}
public static void main(String[] args) {
ServerSocketTest serverT=new ServerSocketTest();
serverT.doLister(); }
public void doLister(){
System.out.println("Socket服务器已经启动啦");
ServerSocket ssk=null;
try{
ssk=new ServerSocket(8088);
while(true){
Socket client=ssk.accept();
SSocket st=new SSocket(client);
new Thread(st).start();
}
}catch (IOException e) {
e.printStackTrace();
}finally{
if(ssk!=null){
try{
ssk.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}线程类代码public class SSocket extends Thread {
Socket sk =null;
public SSocket(Socket socket){
this.sk=socket;
}
public void run(){
//实现数据的接收与发送
BufferedReader input;
PrintWriter output;
try{
input =new BufferedReader(new InputStreamReader(sk.getInputStream()));
output=new PrintWriter(sk.getOutputStream());
String msg=input.readLine();
System.out.println("服务器接受客户端数据为:"+msg);
output.println("输入的内容是:"+msg);
output.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
谢谢啦 拜托啦 是在是找不出原因啦 是不是我的ip地址不对呢 是不是该把我电脑tcp协议ip改了呢,纠结死了啊,哎哎。
public class SocketTest {
public static void main(String[] args) {
Socket sk =null;
try {
Scanner input=new Scanner(System.in);
System.out.println("请输入内容,如果输入f,输入结束:");
String name=input.next();
while(!name.equals("f")){
sk=new Socket("127.0.0.1",8088);
OutputStream out =sk.getOutputStream();
PrintWriter os =new PrintWriter(out);
BufferedReader is =new BufferedReader(new InputStreamReader(sk.getInputStream()));
//想服务器传入数据
os.print(name);
os.flush();
//获取服务器返回的数据
String res=is.readLine();
System.out.println(res);
name=input.next();
//释放资源
os.close();
is.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(sk!=null){
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}}服务器代码
public class ServerSocketTest extends HttpServlet {
public void init() throws ServletException{
super.init();
this.doLister();
}
public static void main(String[] args) {
ServerSocketTest serverT=new ServerSocketTest();
serverT.doLister(); }
public void doLister(){
System.out.println("Socket服务器已经启动啦");
ServerSocket ssk=null;
try{
ssk=new ServerSocket(8088);
while(true){
Socket client=ssk.accept();
SSocket st=new SSocket(client);
new Thread(st).start();
}
}catch (IOException e) {
e.printStackTrace();
}finally{
if(ssk!=null){
try{
ssk.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}线程类代码public class SSocket extends Thread {
Socket sk =null;
public SSocket(Socket socket){
this.sk=socket;
}
public void run(){
//实现数据的接收与发送
BufferedReader input;
PrintWriter output;
try{
input =new BufferedReader(new InputStreamReader(sk.getInputStream()));
output=new PrintWriter(sk.getOutputStream());
String msg=input.readLine();
System.out.println("服务器接受客户端数据为:"+msg);
output.println("输入的内容是:"+msg);
output.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
谢谢啦 拜托啦 是在是找不出原因啦 是不是我的ip地址不对呢 是不是该把我电脑tcp协议ip改了呢,纠结死了啊,哎哎。
//客户端代码改一下:// 想服务器传入数据
os.println(name);//写入回车换行//因为在服务端
String msg=input.readLine();
//如果没有回车,服务端不知道客户端的输入什么时候结束,就会一直等到回车的出现...