javaSocket客户端与C服务端通信
转自:http://hi.baidu.com/ssrt_hanbing/blog/item/62e3b934598eeb82a71e1238.html
通过高低位转换。
package com.commnt;
import java.net.*;
import java.io.*;
public class Client {
public String send(String address, int port, String str) {
OutputStream os = null;
DataInputStream is = null;
String look = "";
Socket socket = null;
try {
socket = new Socket(address, port);
os = socket.getOutputStream();
byte[] phone = new byte[str.length()];
phone = ByteUtil.StringtoBytes(phone, str);
os.write(phone);
os.flush();
is = new DataInputStream(socket.getInputStream());
byte[] phone1 = new byte[str.length()];
is.read(phone1);
String strBype = new String(phone1);
look = strBype.toString();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(socket, is, os);
}
return look;
}
public void close(Socket socket, DataInputStream is, OutputStream os) {
if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if (socket != null)
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ByteUtil
相关文档:
大家好,这里有IBM的三个长期需求:均是需要2年以上相关工作经验,其中Java以及Testing需要英语可以交流,C/Unix不需要语言。
Java 大连
描述:Java programming, knowledge in J2SE, SWT/JFace, XML. Eclipse programming, knowledge in eclipse architecture. Clear understanding of plugin development. Hands-on exp ......
extern是C/C++语言中表明函数和全局变量作用范围(可见性).
它告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。
1。对于extern变量来说,仅仅是一个变量的声明,其并不是在定义分配内存空间。如果该变量定义多次,会有连接错误
2。通常,在模块的头文件中对本模块提供给其它模块引用的函数和 ......
用宏实现一个swap功能
#include <stdio.h>
#include <stdlib.h>
#define SWAP( TYPE,ARG1,ARG2 ) \
void TYPE##Swap( TYPE *p, TYPE *q ) \
{ \
TYPE tmp = *p; \
*p = *q; \
*q = tmp; \
} \
TYPE##Swap(&ARG1,&ARG2 ......
/*
思路:递归算法
前0..cur-1位置上已经排好,当前cur位置取一个和前面都不一样的,然后递归处理后面的。
*/
/* 输出1,2,3,..,n的排列数 */
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
void p(int n)
{
extern void _p(int n, int cur, int *a);
int *a;
a = ......
/*
这是个常见的面试题哦,总之我面试的时候遇到过, 当时没有答上来
回去后想出来下面的方法一,该法还有个附加优点,可以判断出链表在哪个地方形成环的(即如果想拆开这个环,从哪个地方断开)。
后来知道还有个经典算法,即使用两个指针,一快一慢向前试探,如果最终重合则链表有环,果然优美的算法。
*/
#inc ......