c java socket_c 和 java 通过 socket 进行通信

socket 端口占用 命令:

1. netstat -anpt

现在使用C 的server 端 和java 的client 端进行简单的通信。

server.c

/*

============================================================================

Name : server.c

Author : king

Version :

Copyright : Your copyright notice

Description : Hello World in C, Ansi-style

============================================================================

*/

#include

#include

#include

#include

#include

#include /* inet(3) functions */

#include

#include

#include

#include

int handle(int point);

int main(void) {

int sfd, ind;

struct sockaddr_in addr;

struct sockaddr_in clent;

char resv[1024], sendbuf[1024];

char buf[1024];

char * myaddr = "127.0.0.1";

int ret; // 返回值设置

socklen_t lent;

int pid;

addr.sin_family = AF_INET; //IPv4 Internet protocols

addr.sin_port = htons(5050); //这里输入服务器端口号

addr.sin_addr.s_addr = inet_addr(myaddr);

; //INADDR_ANY表示本机IP

//獲取socket描述符,IPV4asd

printf("socket start \n");

sfd = socket(AF_INET, SOCK_STREAM, 0);

if (sfd < 0) {

printf("socket error \n");

return -1;

}

printf("bind start \n");

//将套接子与指定端口链接

if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {

printf("bind error \n");

return -1;

}

//监听套接子

printf("listen start \n");

if (listen(sfd, 1024) < 0) {

printf("listen error \n");

return -1;

}

for (;;) {

//接受来自客户端的信息

printf("accept start \n");

memset(&clent, 0, sizeof(clent));

lent = sizeof(clent);

ind = accept(sfd, (struct sockaddr *) &clent, &lent);

if (ind < 0) {

printf("accept error %d \n", ind);

return -1;

}

printf("infor \n");

printf("clent addr%s porit %d\n",

inet_ntop(AF_INET, &clent.sin_addr, buf, sizeof(buf)),

ntohs(clent.sin_port));

pid = fork();

if (pid == 0) {

//子进程

close(sfd);

handle(ind);

} else if (pid < 0) {

//error

close(ind);

} else {

//父进程

}

}

return EXIT_SUCCESS;

}

int handle(int point) {

int retn;

char buf[1024];

for (;;) {

retn = read(point, buf, sizeof(buf));

if (retn < 0) {

printf("read error \n");

close(point);

break;

} else if (retn == 0) {

printf("client exit \n");

close(point);

break;

}

printf("client:%s\n", buf);

if (strcmp("exit", buf) == 0) {

printf("exit \n");

close(point);

return 0;

}

}

return 0;

}

client.c

package HA.Socket;

import java.io.*;

import java.net.*;

public class SocketClient {

static Socket client;

public SocketClient(String site, int port){

try{

client = new Socket(site,port);

System.out.println("Client is created! site:"+site+" port:"+port);

}catch (UnknownHostException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}

}

public String sendMsg(String msg){

try{

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

PrintWriter out = new PrintWriter(client.getOutputStream());

out.println(msg);

out.flush();

return in.readLine();

}catch(IOException e){

e.printStackTrace();

}

return "";

}

public void closeSocket(){

try{

client.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

package HA.Socket;

public class TestSocketClient {

public static void main(String[] args){

SocketClient client = new SocketClient("127.0.0.1",5050);

System.out.println(client.sendMsg("test1111111111"));

client.closeSocket();

}

}

补充说明: 服务器端和客户端 都要使用相同的端口号。

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/tianxuhong/article/details/46743523

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值