本文共 5040 字,大约阅读时间需要 16 分钟。
继上一篇博客《Java与Python套接字Socket通信的例子》,今晚又完成了Java与C语言套接字通信过程,经过一些调试,最后成功了。Github根链接是:
——惠州学院13网络工程 吴成兵 20160625
fgets(message,30,stdin);
:用fgets输入数据BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String string=br.readLine();if(string==null)continue;
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
pw.println(string);
Server.java
文件,并存放Java服务器端代码放;新建client.c
,并存放C代码。javac Server.java
,编译服务器端代码,生成Server.class
;输入gcc client.c -o client
,编译客户端代码,生成client
可运行文件。java Server
运行服务器端,输入./client 127.0.0.1 9955
运行客户端。 /************************************************************************* > File Name: client.c > Author: Wu_Being > Mail: 1040003585@qq.com > Created Time: 2016年06月25日 01:30 > Description: ************************************************************************/#include#include #include #include #include #include void error_handling(char *message);int main(int argc, char *argv[]){ int server_sock; struct sockaddr_in server_addr; char message[30]; int str_len = 0; if(3 != argc){ printf("Usage : %s \n", argv[0]); exit(1); } server_sock = socket(PF_INET, SOCK_STREAM, 0); if(-1 == server_sock){ error_handling("socket() error!"); exit(1); } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(argv[1]); server_addr.sin_port = htons(atoi(argv[2])); if( -1 == connect(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) ){ error_handling("connect() error!"); } printf("C:connect to server success...\n"); printf("C:input data(with 'end' for exit the programing)\n"); int goon=1; //建立连接之后的处理逻辑 while(goon){ printf("C:please input the data :"); memset(message, 32, sizeof(message)); //scanf("%s",message);//unsuccess!!! fgets(message,30,stdin);//success... printf("C:send the data: %s",message); write(server_sock, message, strlen(message)); str_len = read(server_sock, message, 30-1); message[str_len]=0; if(-1 == str_len){ error_handling("read() error!"); } //printf("%d\n",str_len); if(strncmp("end",message,3)==0){ goon=0; printf("C:end....\n\n"); } else{ printf("C:Message from server : %s \n", message); } } //断开连接,关闭套接字 close(server_sock); return 0;}void error_handling(char *message){ fputs(message, stderr); fputc('\n', stderr); exit(1);}
import java.io.*;import java.net.*;import java.util.*;public class Server { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Swait for connect......"); try { ServerSocket serverSocket = new ServerSocket(9955); Socket socket=serverSocket.accept(); System.out.println("S connet from:"+socket.getInetAddress().getHostAddress()); //DataInputStream dis=new DataInputStream(socket.getInputStream());//dis.readUTF();// un success! //DataOutputStream dos=new DataOutputStream(socket.getOutputStream());//dos.writeUTF(string);dos.flush(); BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));//br.readLine();// success... PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);//pw.println(string); boolean goon=true; while(goon){ //String string=dis.readUTF(); String string=br.readLine(); if(string==null)continue; System.out.print("S:receive data:("+string+")"); if(string.equals("end")==false){ string=dealWith(string); //dos.writeUTF(string); //dos.flush(); pw.println(string); System.out.println(" ,S:计算结果("+string+") sent..."); }else{ goon=false; //dos.writeUTF("end"); //dos.flush(); pw.println("end"); System.out.println("\n"); } } serverSocket.close(); //dis.close(); //dos.close(); br.close(); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public static String dealWith(String string){ double radius=0.0; try { radius=Double.parseDouble(string); } catch (NumberFormatException e) { return "NumberFormatException"; } if(radius<0)return "data can't less 0"; double area=radius*radius*Math.PI; return Double.toString(area); }}
Wu_Being博客声明:本人博客欢迎转载,请标明博客原文和原链接!谢谢!
《Java与C语言套接字Socket通信的例子》:如果你看完这篇博文,觉得对你有帮助,并且愿意付赞助费,那么我会更有动力写下去。