第一個Netty程序——構建和運行Echo服務器和客戶端

在構建之前,需要安裝開發環境:JDK和Apache Maven以及IDE。

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.netty</groupId>
  <artifactId>netty</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-resources-plugin</artifactId>
	    <version>2.7</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
	<dependency>
	    <groupId>io.netty</groupId>
	    <artifactId>netty-all</artifactId>
	    <version>4.1.9.Final</version>
	</dependency>
	
  </dependencies>
  <build>
  	<pluginManagement>
  		<plugins>
  			<plugin>
  				<artifactId>maven-compiler-plugin</artifactId>
  				<version>3.2</version>
  				<configuration>
  					<source>1.8</source>
  					<target>1.8</target>
  				</configuration>
  			</plugin>
  			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
            </plugin>
  			<plugin>
  				<groupId>org.codehaus.mojo</groupId>
  				<artifactId>exec-maven-plugin</artifactId>
  				<version>1.6.0</version>
  			</plugin>
  		</plugins>
  	</pluginManagement>
  </build>
</project>

運行構建

①在項目名上右鍵,run as -> mvn clean

將會輸出一系列代碼日誌。如果出現 BUILD SUCCESS說明成功。

運行Echo服務器和客戶端

①並排打開兩個控制檯窗口(cmd)

②一個進到netty\Server目錄中,另一個進到netty\Client目錄中。

③執行命令運行服務端 mvn exec:java -Dexec.mainClass="netty.server.EchoServer" -Dexec.args="9999"

④執行命令運行客戶端 mvn exec:java -Dexec.mainClass="netty.client.EchoClient" -Dexec.args="localhost 9999"

如果出現“Server received:Netty rocks”說明成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章