新手请教JAVA,SOCKET网络编程
package com.cn.socket;
import java.io.*;
import java.net.*;
public class TcpServer {
// 服务器端源程序tcpserver.Java
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
ServerSocket svrsoc=null;
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
InetAddress clientIP=null;
String str=null;
try{
svrsoc=new ServerSocket(8000);
System.out.println("Server start....");
soc=svrsoc.accept();
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
clientIP=soc.getInetAddress();
System.out.println("Client's IP address:"+clientIP);
out.println("welcome.....");
str=in.readLine();
while (!str.equals("quit")){
System.out.println("Client said:"+str);
str=in.readLine();
}
System.out.println("Client want to leave");
}
catch(Exception e){
System.out.println("error:"+e);
}
finally{
in.close();
out.close();
soc.close();
svrsoc.close();
System.exit(0);
}
}
}
以上是服务端代码
package com.cn.socket;
import java.io.*;
import java.net.*;
public class TcpClient {
// 客户端源程序tcpclient.java
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
DataInputStream sysin=null;
String strin=null;
String strout=null;
try{
soc=new Socket(args[0],8000);
System.out.println("Connecting to the Server");
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
strin=in.readLine();
System.out.println("Server said:"+strin);
sysin=new DataInputStream(System.in);
strout=sysin.readLine();
while (!strout.equals("quit")){
out.println(strout);
strout=sysin.readLine();
}
out.println(strout);
}
catch(Exception e){
System.out.println("error:"+e);
}
finally{
in.close();
out.close();
soc.close();
sysin.close();
System.exit(0);
}
}
}
以上是客户端代码,我把服务端,客户端的代码都放到一个包里,
然后我在本机上运行它们,我先运行服务端,有运行程序,然后
运行客户端代码,并且给了相应的IP地址(我是用Eclipse工具),结果客户端,
服务端都报错,错误如下:
error:java.net.BindException: Address already in use: JVM_Bind
Exception in thread "main" java.lang.NullPointerException
at com.cn.socket.TcpServer.main(TcpServer.java:40)
请问我该怎么运行这个程序。
...全文
相关推荐
java网络编程(第三版)oreilly
总而言之,《Java网络编程(第三版)》是一本全面覆盖Java网络编程领域的书籍,它将帮助你掌握从基本的Socket通信到高级的网络应用开发的全套技能,无论你是新手还是资深开发者,都能从中受益匪浅。
Java——UDP,tcp聊天室,基本功能实现,适合新手,发送表情,字体
通过分析和实践这个项目,新手可以掌握网络编程的基础,为进一步深入学习打下坚实的基础。
Java网络编程从入门到精通
无论是新手还是有经验的开发者,都可以从中受益,提升自己在网络编程领域的专业能力。
java网络编程
### 结论《Java网络编程》是一部详尽的指南,适合各种层次的读者,无论是网络开发老手还是Java新手。
基于Java Socket编程的局域网聊天程序源代码
通过分析这个项目中的源代码,新手可以了解到Java网络编程的基本流程,包括如何设置服务器端监听、客户端连接、数据的发送和接收,以及如何设计简洁易用的用户界面。


Eclipse
